I'm using a loop in wordpress to output posts. I want to wrap every three posts inside of a div. I want to use a counter to increment on each iteration of the loop but I'm not sure of the syntax that says "if $i is a multiple of 3" or "if $i is a multiple of 3 - 1".
$i = 1;
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
// If is the first post, third post etc.
if("$i is a multiple of 3-1") {echo '<div>';}
// post stuff...
// if is the 3rd post, 6th post etc
if("$i is a multiple of 3") {echo '</div>';}
$i++; endwhile; endif;
How do I make this happen? thanks!
Why not do the following? This will open it and close it after the third post. Then close the ending div in the event there is not a multiple of 3 to display.
In case you didn't know,
%
is the modus operator will return the remainder after the two numbers are divided.if you dont need extra div you can use this :
Use the modulus operator:
In your code you can use:
and