• 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

Grid Loop Classes (Issue)

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 › Grid Loop Classes (Issue)

This topic is: resolved

Tagged: grid loop, loop_counter

  • This topic has 4 replies, 2 voices, and was last updated 11 years, 11 months ago by Calvin Koepke.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • June 27, 2014 at 4:33 pm #111809
    Calvin Koepke
    Member

    So, I currently have a grid loop showing on a custom page template. The full code is seen below:

    // Do the Custom Loop
    remove_action('genesis_loop', 'genesis_do_loop');
    add_action( 'genesis_loop', 'sf_custom_loop' );
    remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
    remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
    remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
    remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
     
    function sf_custom_loop() {
    
                //WP Query Start
     
                $per_page = 12;
     
                $product_args = array(
                    'post_type' => 'work',
                    'posts_per_page' => $per_page,
                                    'paged' => get_query_var( 'paged' )
                );
                $products = genesis_custom_loop( $product_args );
    }
     
    //Add Post Class Filter
    add_filter('post_class', 'sf_post_class');
    function sf_post_class($classes) {
        global $loop_counter;
        $classes[] = 'one-fourth';
        if ($loop_counter % 4 == 0) {
            $classes[] .= 'first ';
        }
        return $classes;
    }
     
    /** Move Post Info */
    remove_action('genesis_before_post_content','genesis_post_info');
    remove_action('genesis_after_post_content','genesis_post_meta');
     
    genesis();

    It works great, except the loop_counter is applying a "first" class to every single object, not every fourth post in the grid. You'll have to forgive my lack of knowledge with this particular PHP function, as I grabbed this snippet from another Genesis developer.

    You can see what I mean by going to http://stage.clvnk.co/recent-work/ and inspecting each .entry. The password to view the site is "seeme123"

    Any help on this would be greatly appreciated, as well as some explanation to the $loop_counter variable!


    – Calvin Makes ( @cjkoepke )

    http://stage.clvnk.co/recent-work/
    June 27, 2014 at 4:40 pm #111810
    Gary Jones
    Member

    As you're using HTML5, the $loop_counter isn't incremented by Genesis (it's a Genesis global variable, not WP).

    The reason it wasn't implemented in HTML5 mode, is that WP already has it's own counter which does exactly the same thing.

    Try:

    function sf_post_class($classes) {
        global $wp_query;
        $classes[] = 'one-fourth';
        if ($wp_query->current_post % 4 == 0) {
            $classes[] .= 'first ';
        }
        return $classes;
    }
    

    WordPress Engineer, and key contributor the Genesis Framework | @GaryJ

    June 27, 2014 at 4:44 pm #111814
    Calvin Koepke
    Member

    Fantastic! That worked. Thanks!

    Question: why is it only specific to HTML5 mode? Wouldn't using $wp_query make more sense in either case, since it's already in the WordPress core?


    – Calvin Makes ( @cjkoepke )

    June 27, 2014 at 5:02 pm #111816
    Gary Jones
    Member

    Yes $wp_query->current_post can be used in either case, but some folks still use (old XHTML theme) code that relies on $loop_counter, so Genesis keeps it incrementing for backwards compatibility. For forward compatibility (new code, HTML5 mode), it can be dropped, since WP already provides that value.


    WordPress Engineer, and key contributor the Genesis Framework | @GaryJ

    June 27, 2014 at 5:15 pm #111817
    Calvin Koepke
    Member
    This reply has been marked as private.
  • 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