Skip to content

Instantly share code, notes, and snippets.

@billerickson
Forked from GaryJones/functions.php
Created February 9, 2012 15:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save billerickson/1780725 to your computer and use it in GitHub Desktop.
Save billerickson/1780725 to your computer and use it in GitHub Desktop.
Genesis Grid Loop - Parts 1-3
<?php
/**
* Possibly amend the loop.
* Specify the conditions under which the grid loop should be used
*
* @author Bill Erickson
* @link http://code.garyjones.co.uk/genesis-grid-loop-advanced/
*/
function child_maybe_do_grid_loop() {
// Amend this conditional to pick where this grid looping occurs
// This says to use the grid loop everywhere except single posts and pages
return ( ! is_single() && ! is_page() );
}
add_action( 'genesis_before_loop', 'child_prepare_grid_loop' );
/**
* Prepare Grid Loop
* Swap out the standard loop with the grid and apply classes
*
* @author Bill Erickson
* @link http://code.garyjones.co.uk/genesis-grid-loop-advanced/
*/
function child_prepare_grid_loop() {
if( child_maybe_do_grid_loop() ) {
// 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_grid_query' );
/**
* Grid Query
* Any changes to the actual query (posts per page, category…) should be here.
*
* @param object $query
*
* @author Bill Erickson
* @link http://code.garyjones.co.uk/genesis-grid-loop-advanced/
*/
function child_grid_query( $query ) {
// Only apply to main query, if this matches our grid query conditional, and if it isn't in the backend
if( $query->is_main_query() && child_maybe_do_grid_loop() && !is_admin() ) {
// Set your desired grid posts
$posts_per_page = 6;
// Set your desired features on the homepage
$features = 1;
// Don't edit below, this does the logic to figure out how many posts on each page
$offset = 0;
$paged = $query->query_vars['paged'];
if ( 0 == $paged )
// If first page, add number of features to grid posts, so balance is maintained
$posts_per_page += $features;
else
// Keep the offset maintained from our page 1 adjustment
$offset = ( $paged - 1 ) * $posts_per_page + $features;
$query->set( 'posts_per_page', $posts_per_page );
$query->set( 'offset', $offset );
}
}
/**
* Prepare the grid loop.
* Only use grid-specific arguments. All query args should be done in the child_grid_query() function
*
* @author Bill Erickson
* @link http://code.garyjones.co.uk/genesis-grid-loop-advanced/
*
* @uses genesis_grid_loop() Requires Genesis 1.5
*/
function child_do_grid_loop() {
// Grid specific arguments
$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 &#x2192;', 'genesis' ),
);
// Combine with original query
global $wp_query;
$args = array_merge( $wp_query->query_vars, $grid_args );
// Create the Grid Loop
genesis_grid_loop( $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://code.garyjones.co.uk/genesis-grid-loop-advanced/
*
* @global array $_genesis_loop_args
* @global integer $loop_counter
*
* @param array $grid_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