Community Forums › Forums › Archived Forums › General Discussion › Using grid with custom query
- This topic has 4 replies, 2 voices, and was last updated 7 years, 2 months ago by Victor Font.
-
AuthorPosts
-
February 5, 2017 at 7:52 pm #200612JPJP9Member
I'm using the following bit of code to get a grid
//Grid Classes function webtree_grid_post_class( $classes ) { global $wp_query; if( !$wp_query->is_main_query()) return $classes; $columns = 4; $column_classes = array( '', '', 'one-half', 'one-third', 'one-fourth', 'one-fifth', 'one-sixth' ); $classes[] = $column_classes[$columns]; if( 0 == $wp_query->current_post % $columns ) $classes[] = 'first'; return $classes; }
But now that I've change the query on the template to a custom query
function wt_custom_loop() { $custom_terms = get_terms('businessunits'); foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array('post_type' => 'directory', 'tax_query' => array( array( 'taxonomy' => 'businessunits', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); if($loop->have_posts()) { echo '<h2>'.$custom_term->name.'</h2>'; while($loop->have_posts()) : $loop->the_post(); //output endwhile; } } }
The grid has stopped working. I think its something to do with this line
if( !$wp_query->is_main_query())
February 6, 2017 at 9:29 pm #200676JPJP9MemberBy adding this to the top of my loop
do_action( 'genesis_before_entry' ); printf( '<article %s>', genesis_attr( 'entry' ) ); do_action( 'genesis_entry_header' ); do_action( 'genesis_before_entry_content' ); printf( '<div %s>', genesis_attr( 'entry-content' ) );
and this to the bottom:
do_action( 'genesis_after_entry_content' ); do_action( 'genesis_entry_footer' ); echo '</article>'; do_action( 'genesis_after_entry' );
I've managed to get the 'one-fourth' added to the article class but its still omitting the 'first' where relevant
February 7, 2017 at 9:01 am #200701Victor FontModeratorSince $columns is a fixed number, why are you loading all column classes into an array. It's an extra unnecessary step. Also, you're not adding the "first" class. You're overwriting whatever other classes were there. I would refactor that portion of code as:
$columns = 4; $classes[] = 'one-fourth'; if( 0 == $wp_query->current_post % $columns ) $classes[] .= ' first';
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?February 7, 2017 at 4:59 pm #200729JPJP9MemberThanks Victor.
So I've change it to : `
function wt_grid_post_class( $classes ) {
global $wp_query;
if( !$wp_query->is_main_query())
return $classes;$columns = 4;
$classes[] = 'one-fourth';
if( 0 == $wp_query->current_post % $columns )
$classes[] .= ' first';return $classes;
}`
but its still not returning the required 'first' classFebruary 8, 2017 at 3:01 am #200750Victor FontModeratorThen the problem is in this line: if( 0 == $wp_query->current_post % $columns ). I would install the Kint debugger plugin: https://wordpress.org/plugins/kint-php-debugger/ and test the value of calculation. Just before the line executes, I would add the following debug commands.
d($wp_query->current_post); d($wp_query->current_post % $columns); if( 0 == $wp_query->current_post % $columns )
This will display the value of current_post and result of the calculation on the screen so you can see what the code is doing. Remove the debug commands and deactivate the Kint plugin once you've figured out what's wrong.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet? -
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.