I have a query that loops through <li>
‘s and displays the number of <li>
‘s I have. Is there a way I can make the loop pull 2 <li>
‘s at a time rather than one?
// 5 list items
<ul>
<?php while(has_posts()): the_post(); ?>
<li>
<div class="slide"><?php the_content(); ?></div>
</li>
<?php endwhile; ?>
</ul>
output
<ul>
<li><div class="slide">content 1</div></li>
<li><div class="slide">content 2</div></li>
<li><div class="slide">content 3</div></li>
<li><div class="slide">content 4</div></li>
<li><div class="slide">content 5</div></li>
</ul>
and how I would like it to output…
<ul>
<li>
<div class="slide">
content 1
content 2
</div>
</li>
etc...
</ul>
I hope you understand what I am trying to do.
Thanks!
Easy using WP query (get to know it…)
Hope this helps. Let me know if you encounter any issuse.
EDIT 1:
* explenation – just added a small conditional counter
EDIT 2:
* please note i changed has_post to have_post (think it should be that way? your call)
Drawing on Sagive SEO‘s post this also seems to work without using counters.
The WordPress function the_post() advances the $wp_query object post index, just like the next_post() WP_Query object method does. Adding a second call to the_post() affects the value of the have_posts() function.
This loop process two posts per loop instead of one unless an odd number of posts are returned in which case the conditional will be false and only the last post will be processed on the last loop pass.
EDIT:
Some Math for 7 total posts.
$wp_query->post_count = 7
$wp_query->current_post The index of the post currently being displayed (starting with 0).
Pass one. The first
the_post()
initializes$wp_query->current_post
to 0. if ( 0 + 1 < 7 ). The conditionalthe_post()
increments$wp_query->current_post
to 1.Pass two. The first
the_post()
increments$wp_query->current_post
to 2. if ( 2 + 1 < 7 ). The conditionalthe_post()
increments$wp_query->current_post
to 3.Pass three. The first
the_post()
increments$wp_query->current_post
to 4. if ( 4 + 1 < 7 ). The conditionalthe_post()
increments$wp_query->current_post
to 5.Pass four. The first
the_post()
increments$wp_query->current_post
to 6. if ( 6 + 1 < 7 ). The conditional is false and does not execute.