I’m having a problem with the WordPress posts loop. I’d like to break down the post loop, to add some HTML div tags in the post. For example, I’m trying to add a div .wrap
around the posts after first post. As it’s to wrap the posts, so it should be only once.
The problem is that the .wrap
div is being repeated because of the loop. How can I break the loop, and add the .wrap
div only once and then continue the loop?
Here’s my code:
while ($wp_query->have_posts()) :
$wp_query->the_post();
if($wp_query->current_post <= 1){ ?>
<div class="x">
.. post 1
</div>
<?php }
if($wp_query->current_post > 1){ ?>
<div class="wrap"> <!-- should be only once -->
<div class="y">
.. post 2
</div>
<div class="y">
.. post 3
</div>
</div><!-- should be only once -->
<?php } ?>
endwhile;
Try this
You can just use an iterator index, like this:
You will get an empty
<div class="x" />
if there’s no post and an empty<div class="y" />
if there’s only one post. But may be that’s feasible in your case…edit: example
prints