Some people may be experiencing some issues with the Genesis Grid Loop in Genesis 1.9 (and rightly so).
[php]
if ( function_exists( 'genesis_grid_loop' ) ) {
genesis_grid_loop(
array(
'features' => 0,
'feature_image_size' => 0,
'feature_image_class' => 'alignright post-image',
'feature_content_limit' => 0,
'grid_image_size' => 'thumbnail',
'grid_image_class' => 'alignleft',
'grid_content_limit' => 250,
'more' => __( '[Read more...]', 'child-domain' ),
'posts_per_page' => 25,
/** Remove categories from loop */
'category__not_in' => array( 1, 2, 3, )
)
);
} else {
genesis_standard_loop();
}
[/php]
However, with Genesis 1.9, this will no longer work. Instead you must modify the query as you should via pre_get_posts
hook. Bill Erickson has a great tutorial on how to do customize the WordPress Query, which I will also re-iterate here.
Custom Grid Loop Conditional
First, I love the Grid Loop setup that Gary and Bill did here. However, both Gary and Bill admit that there are other better options than using the genesis_grid_loop() such as Bill's Better, Easier Grid Loop.
However, some sites are just not worth re-doing. So in the meantime a fix is necessary. So here it is. Personally, I would use the function child_is_doing_grid_loop()
from the Advanced Grid Loop. This will be the function where you declare all the places that you want the Grid Loop to appear using the WordPress Conditionals. This will keep your code organized.
<?php | |
/** | |
* Possibly amend the loop. | |
* | |
* Specify the conditions under which the grid loop should be used. | |
* | |
* @author Bill Erickson | |
* @author Gary Jones | |
* @author Travis Smith | |
* @link http://code.garyjones.co.uk/genesis-grid-loop-advanced/ | |
* @link http://wpsmith.net/2013/genesis/genesis-grid-loop-in-genesis-1-9/ | |
* | |
* @return boolean Return true of doing the grid loop, false if not. | |
*/ | |
function wps_is_doing_grid_loop() { | |
// Amend this conditional to pick where this grid looping occurs. | |
// This says to use the grid loop everywhere except single posts, | |
// single pages and single attachments. | |
return ( ! is_singular() ); | |
} |
Modify the WordPress Query Correctly
Then you need to modify the WordPress Query via pre_get_posts
hook. Here's how you can modify the query to exclude a category.
<?php | |
add_action( 'pre_get_posts', 'wps_exclude_cat_in_grid' ); | |
/** | |
* Exclude Category from Grid | |
* | |
* @author Bill Erickson | |
* @author Travis Smith | |
* @link http://www.billerickson.net/customize-the-wordpress-query/ | |
* @link http://wpsmith.net/2013/genesis/genesis-grid-loop-in-genesis-1-9/ | |
* @param object $query WP Query data | |
* | |
*/ | |
function wps_exclude_cat_in_grid( $query ) { | |
if( $query->is_main_query() && wps_is_doing_grid_loop() ) { | |
$query->set( 'cat', '-1,-2' ); | |
} | |
} |
Here's how you can modify the query to limit the query to a category.
<?php | |
add_action( 'pre_get_posts', 'wps_include_cat_in_grid' ); | |
/** | |
* Limit Query to one Category | |
* | |
* @author Bill Erickson | |
* @author Travis Smith | |
* @link http://www.billerickson.net/customize-the-wordpress-query/ | |
* @link http://wpsmith.net/2013/genesis/genesis-grid-loop-in-genesis-1-9/ | |
* @param object $query WP Query data | |
* | |
*/ | |
function wps_include_cat_in_grid( $query ) { | |
if( $query->is_main_query() && wps_is_doing_grid_loop() ) { | |
$query->set( 'cat', '4' ); | |
} | |
} |
Here's how you can modify the query to change the posts_per_page.
<?php | |
add_action( 'pre_get_posts', 'wps_change_posts_per_page_in_grid' ); | |
/** | |
* Change Posts Per Page | |
* | |
* @author Bill Erickson | |
* @author Travis Smith | |
* @link http://www.billerickson.net/customize-the-wordpress-query/ | |
* @link http://wpsmith.net/2013/genesis/genesis-grid-loop-in-genesis-1-9/ | |
* @param object $query WP Query data | |
* | |
*/ | |
function wps_change_posts_per_page_in_grid( $query ) { | |
global $wp_the_query; | |
if( $query->is_main_query() && wps_is_doing_grid_loop() ) { | |
$query->set( 'posts_per_page', '15' ); | |
} | |
} |
Please note that all of this code belongs in functions.php or some other custom file. I personally recommend a custom query.php or grid.php file to keep your code clean.
So back in your template file, you can change the original loop function to:
[php]
if ( function_exists( 'genesis_grid_loop' ) ) {
genesis_grid_loop(
array(
'features' => 0,
'feature_image_size' => 0,
'feature_image_class' => 'alignright post-image',
'feature_content_limit' => 0,
'grid_image_size' => 'thumbnail',
'grid_image_class' => 'alignleft',
'grid_content_limit' => 250,
'more' => __( '[Read more...]', 'child-domain' ),
)
);
} else {
genesis_standard_loop();
}
[/php]
These are the necessary components for displaying the loop, which is modified via pre_get_posts
now.
Michael says
Travis,
I’m currently using the Grid Loop plugin for Genesis by Bill Erickson and have not had any issue with 1.9.1.
I am assuming this GREAT tutorial is for the ones not using the plugin.
Marcel Bootsman says
Thanks for pointing me to the Better, Easier Grid Loop post from Bill Erickson 🙂
Debbie says
Thanks for posting this update. I used the category__not_in and I noticed right after I upgraded, that is was not working the way it once had. I am going to give this update a go.
This past week, I have been reading more and more about WP_Query to better understand.
Thanks again.
Joseph says
What I don’t like about this article is that I realize using genesis can cause your client sites to become unfunctional as genesis updates. You would think they would leave a contingency and make it backward compatible. I am new to genesis and this is a major turn off for me. I thought it was suppose to update itself. It never implied that it could update and render your child theme useless.
Just the kind of thing I want to put on a client site! “Hey guy, I chose to use Genesis in web design so you will probably have to pay me later to fix the inadequacies of the frameworks being backward compatible. Thanks!”
Mike says
Thanks Tavis. I’m actually trying to remove the grid loop. I’ve figured that out for the home page, but my ‘page 2’ of posts shows up in grid format. As a subject guru, any idea on how to configure WP for this? Cheers!
Thierry says
Hi Travis,
I need to change the loop in genesis 1.9.1 to include postmeta data. how can I do that? thanks
Ed Booth says
Hi Travis,
Thanks for your help with this. I have half my problem solved.
Would you happen to know how to create a second query on a page template that would show the posts in the gird loop?
For example I want to create a template page that shows the page content in the featured location and then the 10 most recent posts in the grid loop below it.
Thanks for any help.
Ed
Brian Steck says
Hey Travis,
Thanks for your help here. I’m trying to find the correct hook to modify the title content limit within the genesis grid loop. Any ideas?
Thanks,
Brian
Greg says
Hi Travis,
How would one configure it so it shows 5 posts on the first page (1 feature and 4 grid) and then 10 posts on subsequent pages (10 grid posts)?
Thanks,
Greg
Chris Mower says
Thanks for the tips here. I’m a bit confused though… after implementing this my posts on the category page are not showing up as a grid, but rather are showing up stacked one on top of the other. Any ideas as to why that might be happening?