Community Forums › Forums › Archived Forums › General Discussion › Grid Loop Classes (Issue)
Tagged: grid loop, loop_counter
- This topic has 4 replies, 2 voices, and was last updated 9 years, 9 months ago by Calvin Koepke.
-
AuthorPosts
-
June 27, 2014 at 4:33 pm #111809Calvin KoepkeMember
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 )
June 27, 2014 at 4:40 pm #111810Gary JonesMemberAs 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 #111814Calvin KoepkeMemberFantastic! 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 #111816Gary JonesMemberYes $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 #111817Calvin KoepkeMemberThis reply has been marked as private. -
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.