Site icon WP Smith

Genesis Grid Loop in Genesis 1.9

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.