Created
January 2, 2012 22:27
-
-
Save billerickson/1552401 to your computer and use it in GitHub Desktop.
Genesis Grid Loop - different number on first page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'genesis_before_loop', 'child_maybe_do_grid_loop' ); | |
/** | |
* Before we get to the loop, see if we're anywhere but a single page. If so, | |
* swap out the standard loop for our grid loop. | |
* | |
* @author Gary Jones | |
* @link http://dev.studiopress.com/genesis-grid-loop-advanced.htm | |
*/ | |
function child_maybe_do_grid_loop() { | |
// Amend this conditional to pick where this grid looping occurs | |
if ( ! is_single() && ! is_page() & !is_home() ) { | |
// Remove the standard loop | |
remove_action( 'genesis_loop', 'genesis_do_loop' ); | |
// Use the prepared grid loop | |
add_action( 'genesis_loop', 'child_do_grid_loop' ); | |
// Add some extra post classes to the grid loop so we can style the columns | |
add_filter( 'genesis_grid_loop_post_class', 'child_grid_loop_post_class' ); | |
} | |
} | |
add_action( 'pre_get_posts', 'child_modify_query' ); | |
/** | |
* Modify the query's posts per page | |
* | |
* @author Bill Erickson | |
* @link http://dev.studiopress.com/genesis-grid-loop-advanced.htm | |
* | |
* @param object $query | |
*/ | |
function child_modify_query( $query ) { | |
// Only apply to the main query | |
if( $query->is_main_query() ) { | |
// Use the same conditional you used in child_maybe_grid_loop() | |
if( !is_single() && ! is_page() && !is_home() ) { | |
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; | |
if( 1 < $page ) | |
// Number of posts to show on subsequent pages (just grid) | |
$query->set( 'posts_per_page', '4' ); | |
else | |
// Number of posts to show on first page (grid + features) | |
$query->set( 'posts_per_page', '5' ); | |
} | |
} | |
} | |
/** | |
* Prepare the grid loop. | |
* | |
* Takes care of existing query arguments for the page e.g. if it's a category | |
* archive page, then the "cat" argument is carried into the grid loop, unless | |
* it's overwritten in the $grid_args. | |
* | |
* @author Gary Jones | |
* @link http://dev.studiopress.com/genesis-grid-loop-advanced.htm | |
* @uses genesis_grid_loop() Requires Genesis 1.5 | |
*/ | |
function child_do_grid_loop() { | |
global $wp_query; | |
//print_r( $wp_query ); | |
global $query_string, $paged; | |
// Ensure the arguments for the normal query for the page are carried forwards | |
// If you're using a Page to query the posts (e.g. with the Blog template), comment out the next line. | |
wp_parse_str( $query_string, $query_args ); | |
// Create an array of arguments for the loop - can be grid-specific, or | |
// normal query_posts() arguments to alter the loop | |
$grid_args = array( | |
'features' => 1, | |
'feature_image_size' => 0, | |
'feature_image_class' => 'alignleft post-image', | |
'feature_content_limit' => 0, | |
'grid_image_size' => 'grid-thumbnail', | |
'grid_image_class' => 'alignleft post-image', | |
'grid_content_limit' => 0, | |
'more' => __( 'Continue reading →', 'genesis' ), | |
//'posts_per_page' => 3, | |
); | |
// Make sure the first page has a balanced grid | |
/* | |
if ( 0 == $paged ) | |
// If first page, add number of features to grid posts, so balance is maintained | |
$grid_args['posts_per_page'] += $grid_args['features']; | |
else | |
// Keep the offset maintained from our page 1 adjustment | |
$grid_args['offset'] = ( $paged - 1 ) * $grid_args['posts_per_page'] + $grid_args['features']; | |
*/ | |
// Merge the standard query for this page, and our preferred loop arguments | |
genesis_grid_loop( array_merge( (array) $query_args, $grid_args ) ); | |
} | |
/** | |
* Add some extra body classes to grid posts. | |
* | |
* Change the $columns value to alter how many columns wide the grid uses. | |
* | |
* @author Gary Jones | |
* @link http://dev.studiopress.com/genesis-grid-loop-advanced.htm | |
* | |
* @global array $_genesis_loop_args | |
* @global integer $loop_counter | |
* @param array $classes | |
*/ | |
function child_grid_loop_post_class( $grid_classes ) { | |
global $_genesis_loop_args, $loop_counter; | |
// Alter this number to change the number of columns - used to add class names | |
$columns = 3; | |
// Only want extra classes on grid posts, not feature posts | |
if ( $loop_counter >= $_genesis_loop_args['features'] ) { | |
// Add genesis-grid-column-? class to know how many columns across we are | |
$grid_classes[] = sprintf( 'genesis-grid-column-%s', ( ( $loop_counter - $_genesis_loop_args['features'] ) % $columns ) + 1 ); | |
// Add size1of? class to make it correct width | |
$grid_classes[] = sprintf( 'size1of%s', $columns ); | |
} | |
return $grid_classes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment