• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Using grid with custom query

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Using grid with custom query

This topic is: not resolved
  • This topic has 4 replies, 2 voices, and was last updated 9 years, 3 months ago by Victor Font.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • February 5, 2017 at 7:52 pm #200612
    JPJP9
    Member

    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 #200676
    JPJP9
    Member

    By 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 #200701
    Victor Font
    Moderator

    Since $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 #200729
    JPJP9
    Member

    Thanks 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' class

    February 8, 2017 at 3:01 am #200750
    Victor Font
    Moderator

    Then 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?

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble