Is it possible to require a minimum number of results in a wordpress loop? And if the loop doesn't return enough posts, then you show a placeholder?
Basically, I need to show 8 posts in a grid. If there aren't enough posts to fill all 8 spaces, then I want to show a gray box where a post should be. So if there are only 6 posts, there will be 6 post boxes and 2 gray empty boxes. It seems like I need to:
- Count how many posts were returned in the loop
- If there are less than 8 posts, then subtract that count from 8
- Add X number of placeholders at the end of the loop
I'm just not sure how/where to add the correct number of placeholders. Here's what I've got:
<?php $args= array(
'post_type' => 'beer',
'posts_per_page' => -1
);
$beer2_query = new WP_Query( $args ); if ( $beer2_query->have_posts() ) :
$count = $beer2_query->post_count;
$i = 1; ?>
<div class="row"
<?php while ( $beer2_query->have_posts() ) : $beer2_query->the_post(); ?>
<div class="col-sm-3">
<div class="card">
<h3><?php the_title(); ?></h3>
</div>
</div>
<?php $i++; endwhile; wp_reset_postdata(); ?>
<?php if ($count < 8) {
$placeholder_count = (8 - $count);
// how do I echo out placeholder boxes that equals the placeholder_count total?
} ?>
</div>
<?php endif; ?>
Thank you!