Using grid for search results
-
CreatorTopic
-
May 25, 2013 at 3:32 pm #8530
askwpgirl
ParticipantHi Jason,
Is it possible to get the grid to work for search results page? I have modified the template parts in my functions file to use the grid for the search results, but the counting doesn’t work, of course, since the content-grid.php is being called from inside the search.php page which doesn’t have the query to count the posts, so the grid displays, but it’s formatting is wonky.
When I copy in the query to count the posts from the template_grid.php or archive page, there is an error for “division by zero.” Any suggestions on how to modify the search.php file to count the results?
Thanks!
A
-
CreatorTopic
-
AuthorReplies
-
May 25, 2013 at 4:47 pm #8531
Jason Bobich
KeymasterThere are these functions that were added when Jump Start was released that allow you to pass variables through the template files through a global variable the theme holds. These are
themeblvd_set_att()
andthemeblvd_get_att()
. When a post-grid is setup, these template attributes are set in the loop.There are probably a lot of ways you could do this, but here’s what I’m thinking — You’ll need to set a counter yourself for this situation. I would put content-search_results.php back as your template part for search results and copy everything from content-grid.php to it as a starting point.
First set the counter to 1 for the first post.
function my_set_counter() { if( is_search() ) themeblvd_set_att('counter', 1); } add_action('themeblvd_content_top', 'my_set_counter');
Now, within the file, you can increment your counter by modifying the code a little.
<?php /** * The template used for displaying ... */ $counter = themeblvd_get_att( 'counter' ); ?> <div class="grid-item column grid_4<?php if( $counter % 3 == 0 ) echo ' last'; ?>"> ... </div><!-- .grid-item (end) --> <?php themeblvd_set_att( 'counter', $counter++ ); ?>
May 25, 2013 at 5:10 pm #8533askwpgirl
ParticipantI know there should be a content-search_results.php in Jump Start parent, but there is only content-search.php, which is the search template. The content-search_results.php file seems to have gone MIA between releases. The old content-search_results.php file had this odd comment at the top:
The default template for displaying content in blogroll. Is there an updated content-search_results.php file for the latest Jump Start?
May 25, 2013 at 5:22 pm #8537Jason Bobich
KeymasterAw, yeah that makes sense. Sorry for the confusion. By default the “search_results” template key points to content-archive.php. So it is correct that no content-search-results.php should exist.
So, just make this “search_results” template part ID point to some other file that is a copy of content-grid.php.
For example:
Step 1: Copy content-grid.php and name it “content-search_grid.php”
Step 2: Filter it in:
function my_template_parts( $parts ) { $parts['search_results'] = 'search_grid'; return $parts; } add_filter( 'themeblvd_template_parts', 'my_template_parts' );
Step 3: Start your counter as described in previous comment.
function my_set_counter() { if( is_search() ) themeblvd_set_att('counter', 1); } add_action('themeblvd_content_top', 'my_set_counter');
Step 4: Do what I was talking about previously with the new “content-search_grid.php” file.
<?php /** * The template used for displaying ... */ $counter = themeblvd_get_att( 'counter' ); ?> <div class="grid-item column grid_4<?php if( $counter % 3 == 0 ) echo ' last'; ?>"> ... </div><!-- .grid-item (end) --> <?php themeblvd_set_att( 'counter', $counter++ ); ?>
May 25, 2013 at 5:44 pm #8538askwpgirl
ParticipantYeah, I figured that. I got it all set up. The grid shows up fine, but it’s not counting the posts, so I’m not getting the ‘last’ class set, which is breaking the layout.
I’ve echoed some text in the function to be sure the my_set_counter function was loading in the themeblvd_content_top, which it is, but it seems the counter isn’t counting.
May 25, 2013 at 5:48 pm #8539askwpgirl
ParticipantHere’s my functions:
/*-------------------------------------------------------*/ /* Show Grid for all Archive pages /*-------------------------------------------------------*/ function my_template_parts( $parts ) { // Different archives if( is_archive('') ) $parts['archive'] = 'grid'; if( is_home('') ) $parts['grid_paginated'] = 'grid'; if( is_search('') ) $parts['search_results'] = 'search_results'; return $parts; } add_filter( 'themeblvd_template_parts', 'my_template_parts' ); /*-------------------------------------------------------*/ /* Set counter for search results grid /*-------------------------------------------------------*/ function my_set_counter() { if( is_search() ) themeblvd_set_att('counter', 1); } add_action('themeblvd_content_top', 'my_set_counter');
Here’s my content-search_results.php page (which I verified is being used):
<?php /** * The template used for displaying ... */ $counter = themeblvd_get_att( 'counter' ); ?> <div class="grid-item column grid_4<?php if( $counter % 3 == 0 ) echo ' last'; ?>"> <div class="article-wrap"> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <div class="entry-content"> <?php include (STYLESHEETPATH . '/content-listing.php'); ?> <?php include (STYLESHEETPATH . '/content-graduate.php'); ?> </div><!-- .entry-content --> </article><!-- #post-<?php the_ID(); ?> --> </div><!-- .article-wrap (end) --> </div><!-- .grid-item (end) --> <?php themeblvd_set_att( 'counter', $counter++ ); ?>
May 25, 2013 at 5:53 pm #8541askwpgirl
ParticipantHere’s the search page with a search using the “Custom Search” field which is the WordPress search widget:
http://directory.psychologyofeating.com/?s=binge
Here’s the main directory page:
http://directory.psychologyofeating.com/graduate-directory-view/
If you use the select lists for a search of the custom taxonomies, then, the template that is used is the normal grid vis a vis the archive page, so the display is fine:
http://directory.psychologyofeating.com/category/graduate-directory/paleo-diet/?state
May 25, 2013 at 6:04 pm #8542Jason Bobich
KeymasterHmm, so let’s figure out if the problem is in setting the original counter or it not incrementing properly.
At the top of content-search_results.php, echo out the $counter.
<?php echo $counter; ?>
If it outputs 1, it means the problem is how we’re incrementing it. If it outputs nothing it means that we’re not setting it right from
my_set_counter()
May 25, 2013 at 6:32 pm #8544Jason Bobich
KeymasterThis might be a time to introduce something that I haven’t really documented and is being improved for the next update a bit before I start sharing it with everyone. But it currently should work for archives and homepage in your version, just not search results.
There is something in the framework called “grid” mode that gets triggered if you filter your template parts a certain way.
You can trigger grid mode like this:
/*-------------------------------------------------------*/ /* Show Grid for all Archive pages /*-------------------------------------------------------*/ function my_template_parts( $parts ) { // If these equal grid, index_grid, or archive_grid, // grid mode is triggered automatically. $parts['archive'] = 'grid'; // No need to use is_archive() here $parts['index'] = 'grid'; // No need to use is_home() here return $parts; } add_filter( 'themeblvd_template_parts', 'my_template_parts' );
Note: The way you’re using conditionals previously in template parts filter will not work because the filter is being applied inside the loop, where these conditionals have now changed. See here. However, in this case, the conditionals are unnecessary anyway because “archive” is only used for archive.php and “index” is only used on the homepage.
May 25, 2013 at 7:06 pm #8551askwpgirl
ParticipantThat makes sense, though, what do you suggest to get the counter to work for the current search page?
May 25, 2013 at 7:45 pm #8553Jason Bobich
KeymasterI can’t think of any easy solution. You’d need to setup search.php (like archive.php) to work as a grid. And then you’d have to set all the template atts (which currently only happen if is_archive() or is_home() in grid mode).
In Jump Start 1.1, I’ll have this all setup so you can use grids in search results also.
May 25, 2013 at 10:51 pm #8565askwpgirl
ParticipantI actually did that — set up search.php just like archive, but it was getting a count of zero. I did that first as that seemed the no-brainer thing to do, but for some reason the search wouldn’t work with the counter as it would return 0 posts to the counter, so I wrote to you. I know this is totally off topic for Jump Start. I’ll keep searching for some way to get a counter to work with the search. Search must run a different type of query, so that’s why the archive.php template code won’t work. More WordPress fun.
May 25, 2013 at 11:02 pm #8566askwpgirl
ParticipantOkay, got it to work. Used the archive.php as the code for search.php and the specified the columns, and it works perfectly! That was easy. I don’t know why I didn’t just remove the $columns and substitute with the number 3 to begin with. Here’s the code:
<!-- CONTENT (start) --> <div id="content" class="<?php echo themeblvd_get_column_class('content'); ?> clearfix" role="main"> <div class="inner"> <?php themeblvd_content_top(); ?> <div class="post_list post_list_paginated archive search-results"> <?php global $more; $more = 0; $counter = themeblvd_set_att( 'counter', 1 ); $columns = themeblvd_get_att( 'columns', 3 ); if ( have_posts() ) { while ( have_posts() ) { the_post(); if( $counter == 1 ) themeblvd_open_row(); get_template_part( 'content', themeblvd_get_part( 'search_results' ) ); if( $counter % 3 == 0 ) themeblvd_close_row(); if( $counter % 3 == 0 && themeblvd_get_att( 'posts_per_page' ) != $counter ) themeblvd_open_row(); $counter = themeblvd_set_att( 'counter', $counter+1 ); } if( ($counter-1) != themeblvd_get_att( 'posts_per_page' ) ) themeblvd_close_row(); } else { echo get_template_part( 'content', themeblvd_get_part( 'search' ) );; } ?> <?php themeblvd_pagination(); ?> </div><!-- .post_list (end) --> </div><!-- .inner (end) --> </div><!-- #content (end) --> <!-- CONTENT (end) -->
-
AuthorReplies
- You must be logged in to reply to this topic.