Last active
June 11, 2016 00:06
-
-
Save JiveDig/5556774 to your computer and use it in GitHub Desktop.
Use post_class (post class) to break post into columns
Last example shows how to conditionally use post class, for specific templates or pages
This file contains hidden or 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
/** | |
* Breaks the posts into 4 columns | |
* @link http://www.billerickson.net/code/grid-loop-using-post-class | |
*/ | |
add_filter( 'post_class', 'tsm_archive_post_class' ); | |
function tsm_archive_post_class( $classes ) { | |
global $wp_query; | |
// Keeps columns out of secondary loops ie: Genesis Featured Post widgets | |
if( ! $wp_query->is_main_query() ) { | |
return $classes; | |
} | |
$classes[] = 'one-fourth'; | |
if( 0 == $wp_query->current_post || 0 == $wp_query->current_post % 4 ) | |
$classes[] = 'first'; | |
return $classes; | |
} | |
/** | |
* Breaks the posts into 3 columns | |
* @link http://www.billerickson.net/code/grid-loop-using-post-class | |
*/ | |
add_filter( 'post_class', 'tsm_archive_post_class' ); | |
function tsm_archive_post_class( $classes ) { | |
global $wp_query; | |
// Keeps columns out of secondary loops ie: Genesis Featured Post widgets | |
if( ! $wp_query->is_main_query() ) { | |
return $classes; | |
} | |
$classes[] = 'one-third'; | |
if( 0 == $wp_query->current_post || 0 == $wp_query->current_post % 3 ) | |
$classes[] = 'first'; | |
return $classes; | |
} | |
/** | |
* Breaks the posts into 2 columns | |
* @link http://www.billerickson.net/code/grid-loop-using-post-class | |
*/ | |
add_filter( 'post_class', 'tsm_archive_post_class' ); | |
function tsm_archive_post_class( $classes ) { | |
global $wp_query; | |
// Keeps columns out of secondary loops ie: Genesis Featured Post widgets | |
if( ! $wp_query->is_main_query() ) { | |
return $classes; | |
} | |
$classes[] = 'one-half'; | |
if( 0 == $wp_query->current_post || 0 == $wp_query->current_post % 2 ) | |
$classes[] = 'first'; | |
return $classes; | |
} | |
// Set the number of posts per page | |
add_filter( 'pre_get_posts', 'be_archive_query' ); | |
// @link http://www.billerickson.net/customize-the-wordpress-query/ | |
function be_archive_query( $query ) { | |
if( $query->is_main_query() && $query->is_archive() ) { | |
$query->set( 'posts_per_page', 12 ); | |
} | |
} | |
// OR | |
// If you don't want the change to affect backend | |
// Set the number of posts per page | |
// @link http://www.billerickson.net/customize-the-wordpress-query/ | |
add_filter( 'pre_get_posts', 'mwm_resource_affiliate_archive_query' ); | |
function mwm_resource_affiliate_archive_query( $query ) { | |
if ( ! is_admin() AND ( is_post_type_archive('resource') OR is_post_type_archive('affiliate') ) ) { | |
$query->set( 'posts_per_page', 15 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment