I have created a WordPress loop that that wraps wraps a div around every 3 sets of blog posts. Essentially, it outputs like this:
<div class="row"> // This row does have 3 sets of columns, so it will create another row
<div class="large-4 medium-4 columns panel grid">
<--Content Stuff-->
</div>
<div class="large-4 medium-4 columns panel grid">
<--Content Stuff-->
</div>
<div class="large-4 medium-4 columns panel grid">
<--Content Stuff-->
</div>
</div>
<div class="row"> // Since this row doesn't have three sets of columns, so it does not create another row
<div class="large-4 medium-4 columns panel grid">
<--Content Stuff-->
</div>
<div class="large-4 medium-4 columns panel grid">
<--Content Stuff-->
</div>
</div>
However, I run into issues when I have 3, 6 or 9 blog posts as the loop is creating a new div but has no content to fill it with.
Here is what I currently have – which is working great except for the issue noted above:
<div class="row" data-equalizer>
<?php $i = 1; ?>
<?php query_posts; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if ($community_posts->have_posts()) : while ($community_posts->have_posts()) : $community_posts->the_post(); ?>
<div class="large-4 medium-4 columns" data-equalizer-watch>
<?php get_template_part( 'partials/loop', 'archive-grid' ); ?> // This is the WP Loop
<?php if ($counter % 3 == 0){echo '</div><div class="row" data-equalizer>';} ?>
<?php $counter++ ; endwhile; echo '</div>'; ?>
</div>
<?php else : ?>
<?php get_template_part( 'partials/content', 'missing' ); ?>
<?php endif; ?>
How can I tell the loop to end if a new item does not exist?
You can use the following built-in properties of the
WP_Query()
class:to make your life eaiser, instead of introducing custom counters.
Here’s an example that works on my install:
This should give you the following layouts with three items per row:
and
and so on.
I do not use WordPress, but essentially in the
if
statement you need to check that the next post exists. Change the if statement to:Assuming that
have_posts()
returns whether the next post exists.You can use Break Keyword in php for terminate loop.
And if you want to continue the executions you may use Continue keyword.
Try this code below. Code is not tested. So might need some adjustments.
Since I’ve had the need for something like this several times, I was always trying to solve this by using the counter and modulus operator. This question gave me an interesting idea, that it would be useful to have a custom type of loop that will work with chunks of posts.
This function manipulates
$wp_query
object by changing thepost_count
andcurrent_post
properties and thus controls what will be displayed in the loop. It behaves similar as thehave_posts()
function.Usage in main loop:
Usage with custom $wp_query object:
Right now you’re starting a row before you begin your loop, then ending and starting a new row after cell multiples of three (pseudocode):
You’re having an extra row created because you’re testing to see whether you should end/start rows after you output the cell — if you shift that test to before the cell, you’ll be fine:
Make sense? Your actual code would need to be the following (replacing the entire snippet above):