Reputation: 9664
My goal is to print some extra html, after every four posts.
What is wrong with the following code
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php
$posts = $wp_query->post_count;
if ( $posts == 4 ) {
echo '<div class="clearfix"></div>';
}
?>
<?php endwhile; // end of the loop. ?>
Upvotes: 2
Views: 5367
Reputation: 733
I know this question has already been answered, but I think there are cleaner ways of doing this.
You have several useful properties in the WP_Query object you could use, instead of having to add custom incrementers.
<?php
// Do not forget this part, required if you do not define a custom WP_Query
global $wp_query;
// Below you can find some useful properties in the WP_Query object
// Use them to their full potential
// Gets filled with the requested posts from the database.
echo $wp_query->posts;
// The number of posts being displayed.
echo $wp_query->post_count;
// The total number of posts found matching the current query parameters
echo $wp_query->found_posts;
// The total number of pages. Is the result of $found_posts / $posts_per_page
echo $wp_query->max_num_pages;
// Index of the post currently being displayed. (Can only be used within the loop)
while( have_posts() ) : the_post();
echo $wp_query->current_post;
endwhile;
Your loop would look like this, while using the $current_post property.
<?php global $wp_query; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php
if ( ( $wp_query->current_post + 1 ) % 4 === 0 ) {
echo '<div class="clearfix"></div>';
}
?>
<?php endwhile; // end of the loop. ?>
I believe this to be a good and clean solution.
Read more about the WP_Query object here: https://codex.wordpress.org/Class_Reference/WP_Query
Upvotes: 7
Reputation: 8329
There may be a bug in the answers before mine: it echoes the "closing" tag at the end of every fourth post - but what if the post count is 5? (Or any other number not divisible by 4? Of course the need for solving this problem may vary from HTML to HTML.)
Look at this example with 5:
--post 1--
--post 2--
--post 3--
--post 4--
--closing tag--
--post 5--
??????
You can see that after the 5th post the loop is not closed with the closing tag.
Therefore I would suggest using this code (that takes the number of posts or the number of posts_per_page in account):
<?php
// taking @Jordi Nebot's code from his answer - thank you
if ( have_posts() ) : $count = 1;
?>
<?php while ( have_posts() ) : the_post();?>
<!-- do stuff ... -->
<?php
// the conditions in the if are triggered after every 4th post
// OR if all the posts have been echoed OR all the posts for
// this page have been echoed
if ( $count % 4 == 0 || $wp_query->found_posts == $count || $wp_query->posts_per_page == $count ):
?>
<!-- extra stuff every four posts -->
<?php endif; ?>
<?php $count++; endwhile; ?>
<?php endif; ?>
Upvotes: 0
Reputation: 21
Try this one
<?php
$count = 1;
while ( have_posts() ) : the_post();
wc_get_template_part( 'content', 'product' );
if ( $count%4==0 ) {
echo '<div class="clearfix"></div>';
}
$count++;
endwhile; ?>
Upvotes: 2
Reputation: 3401
Taking the basic code of The Loop, you could include a counter, increment it on every iteration and check if its zero module four.
<?php if ( have_posts() ) : $count = 1; ?>
<?php while ( have_posts() ) : the_post();?>
<!-- do stuff ... -->
<?php if ( $count % 4 == 0 ): ?>
<!-- extra stuff every four posts -->
<?php endif; ?>
<?php $count++; endwhile; ?>
<?php endif; ?>
Upvotes: 7