0

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.

0

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 } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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