I would like to create custom post query where i will list all my posts in the list. Then the tricky bit is to add different content to sixth element off that list.

So something like this

<li>custom post 1</li>
<li>custom post 2</li>
<li>custom post 3</li>
<li>custom post 4</li>
<li>custom post 5</li>
<li>custom post 6</li>
<li>custom post 7</li>

ok here is what it did:

    <?php $my_query = new WP_Query( array( 
        'post_type' => 'people', 
        'posts_per_page' => 10, 
        'order' => 'DESC', 
        'paged'=> $paged
    ) );?>

                        <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

                        <?php if (  $my_query->current_post == 5 ) { ?>
                                <li>I'm odd one</li> 
                        <?php  } else {?>
                                <li><?php the_title(); ?></li>
                         <?php } ?>

                        <?php endwhile; ?>
                        <?php wp_reset_query();?>


I hope that make sense,

Thank you very much.

  • with 'posts_per_page' set to 4, you will never have a need to show something before the sixth post. also, why are you outputting a list element with the title before the while-loop?
    – Michael
    Commented Jun 12, 2012 at 20:20

1 Answer 1


query_posts should only be used for the main loop. Instead, use a new instance of the WP_Query class:

$my_query = new WP_Query( array( 
    'post_type' => 'post', 
    'posts_per_page' => 10, 
    'order' => 'DESC', 
    'paged'=> $paged
) );

echo '<ul>';

while ( $my_query->have_posts() ) : $my_query->the_post();

    if ( $my_query->current_post > 0 && ( $my_query->current_post + 1 ) % 6 == 0 ) {
        echo "<li>I'm odd one</li>";
    } else {
        echo "<li>" . get_the_title() . "</li>";


echo '</ul>';

The above will "do the special thing" every sixth element. Should you just want it on the sixth itself, use $my_query->current_post == 5 instead (index starts at 0).

  • You're current version of this will "do the special thing" on the sixth, eleventh, sixteenth post and so forth. Also, why so many <?php start and end tags? Commented Jun 12, 2012 at 19:10
  • You're right, great catch. The start/end tags are a formatting/readability preference when you're in the midst of HTML. Your way works as well. Thanks!
    – Andy Adams
    Commented Jun 12, 2012 at 19:30
  • I don't see the need for the 'else' as the different content is to be added before every sixth post.
    – Michael
    Commented Jun 12, 2012 at 20:33
  • ok Thanks, but I want this to carry on so list the 6th element which is different from the others and then list will carry on, it looks like with your example my element will always be at the end off the list. I modified my Query above
    – jmysona
    Commented Jun 12, 2012 at 22:41
  • I've updated the code to reflect your problem description. Is there anything missing from it at this point?
    – Andy Adams
    Commented Jun 15, 2012 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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