Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to add some additional HTML markup to specific posts in a loop. For example in the post loop, I want to wrap the post 3, 4, 5 in two divs .extra and .extra2

I want to display post in following order: Post 1, 3, 4, 5, 2

I am trying following:

<?php if ($query ->current_post == 0) { ?>
    <div> Post # 1</div>
<?php } ?>
<?php if ($query ->current_post == 2) { ?>
    <div class="extra">
        <div class ="extra2>
<?php } ?>  
<?php if ($query ->current_post >= 2) { ?>  
        <div> Post # 3</div>
        <div> Post # 4</div>
        <div> Post # 5</div>
 <?php } ?> 
<?php if ($query ->current_post == 1) { ?>
    <div> Post # 2</div>
 <?php } ?>

Now I'm unable to figure out how to close the div extra and extra2 after the post #5 and before the post #2. The number of posts are variable in the loop.

If this method is not correct, please let me know how can I add additional markup for specific posts in the lop. Thanks.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You can simply do it by wrapping closing divs in if condition where current_post is equal to 2. I have added it in following code

<?php if ($query ->current_post == 0) { ?>
<div> Post # 1</div>
<?php } ?>
<?php if ($query ->current_post == 2) { ?>
<div class="extra">
    <div class ="extra2>
<?php } ?>  
<?php if ($query ->current_post >= 2) { ?>  
    <div> Post # 3</div>
    <div> Post # 4</div>
    <div> Post # 5</div>
 <?php } ?> 
<?php if ($query ->current_post == 2) { ?>
    </div>
</div>
<?php } ?>
<?php if ($query ->current_post == 1) { ?>
<div> Post # 2</div>
 <?php } ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.