I need PHP code to add every 2 items inside DIV in WordPress loop.
For example, I need like this:
<div class="wrap">
post
post
</div>
<div class="wrap">
post
post
</div>
<div class="wrap">
post
post
</div>
This is my wordpress loop, but not working, I need every 2 posts inside DIV:
<?php if ( have_posts() ) : // If have post start. ?>
<?php $i = 0; ?>
<?php while ( have_posts() ) : the_post(); // Start Loop: ?>
<?php if ( $i % 2 == 0) : ?>
<div class="wrap">
<?php endif; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_content(); ?>
</article>
<?php if ( $i % 2 == 0 ) : ?>
</div>
<?php endif; ?>
<?php $i++; endwhile; // End Loop. ?>
<?php endif; // If have post end. ?>
Thanks.
if( $wp_query->current_post % 2 == 0 ){...}Or you can set the counter$i=0;outside the loop (if ( have_posts() ) : while ( have_posts() ) : the_post();) and then check if it's modulo 2 is 0:$if( i % 2 == 0)and increment on each iteration ($i++;), beforeendwhile.