php - Two posts in same div - WP loop -
i need following in wordpress:
<div class="posts-wrapped"> <div id="post-1" class="post"> <h1>title 1</h1> </div> <div id="post-2" class="post"> <h1>title 2</h1> </div> </div> <div class="posts-wrapped"> <div id="post-3" class="post"> <h1>title 3</h1> </div> <div id="post-4" class="post"> <h1>title 4</h1> </div> </div>
i know how posts wp loop, how can wrap every 2 posts in .posts-wrapped div?
i fetch posts wp_query.
thanks!
edit: tried few ways it. example this:
$index=0; <div class="posts-wrapped"> <?php while ( have_posts() ) { $index++; ?> <div class="post"> <h1><?php the_post(); ?></h1> </div> <?php if(($index % 2) ==0){ echo '</div><div class="posts-wrapped">'; } } ?> </div>
but prints 1 empty posts-wrapped div:
<div class="posts-wrapped"> lorem ipsum 1<br> lorem 2 ipsum est<br> </div> <div class="posts-wrapped"> lorem ipsum 1<br> lorem 2 ipsum est<br> </div> <div class="posts-wrapped"> </div>
how can rid of last empty div? screws carousel (this content part of carousel).
get index counts loop iteration , check modulus operation know or odd
$index=0; <div class="posts-wrapped"> <?php while ( have_posts() ) { $index++; ?> <div class="post"> <h1><?php the_post(); ?></h1> </div> <?php if(($index % 2) ==0){ echo '</div><div class="posts-wrapped">'; } } ?> </div>
Comments
Post a Comment