Two posts in same div – WP loop

This is really just a matter of logic. You need to conditionally print the div markup. The counter in $wp_query will let you chose every second post.

if (have_posts()) {
  echo '<div class="posts-wrapped">';
    while (have_posts()) {

      the_post();

      if (0 !== $wp_query->current_post 
        && 0 === $wp_query->current_post%2
      ) {
        echo $wp_query->current_post.'</div><div class="posts-wrapped">';
      }

      the_title(); echo '<br />';
    }
  echo '</div>';
}

Leave a Comment