Reputation: 53
I've created a WordPress loop in PHP for a custom post type with 3 columns in the 1st row and 4 columns in the rest of the rows. However, with my current code, the 3rd item from the 1st row repeats itself in the 2nd row which is not ideal. How can I ensure that my 4-column loop starts at the 4th item. Right now it seems to be starting at the 3rd even though I've specified greater than or equal to 4 in my code ($i >= 4). I can't figure out what I'm doing wrong in the code. Any help would be strongly appreciated!
Here is a screenshot of what I mean when I say 3rd item repeats: https://i.sstatic.net/eA0J6.png
Also, here is my current code:
<?php $i = 1 ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if($i <= 3 ): $i++;?>
<div class="col-md-4">
<?php get_template_part( 'loop-templates/content', 'activity' ); ?>
</div>
<?php endif; ?>
<?php if($i >= 4): $i++; ?>
<div class="col-md-3">
<?php get_template_part( 'loop-templates/content', 'activity' ); ?>
</div>
<?php endif; ?>
<?php endwhile; endif; ?>
Upvotes: 2
Views: 336
Reputation: 5098
When $i
is exactly equal to 3, it will be incremented to 4 in the first if
block, then the second if
will also be true. Try this:
<?php if($i <= 3 ): ?>
<div class="col-md-4">
<?php get_template_part( 'loop-templates/content', 'activity' ); ?>
</div>
<?php else: ?>
<div class="col-md-3">
<?php get_template_part( 'loop-templates/content', 'activity' ); ?>
</div>
<?php
endif;
++$i;
?>
Upvotes: 2