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 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><the_content></li>
<li>custom post 6</li>
<li>custom post 7</li>

ok here is what it did:
<ul>

    <?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();?>

    </ul>

I hope that make sense,

Thank you very much.

share|improve this question
    
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 Jun 12 '12 at 20:20
add comment

1 Answer

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>";
    }

endwhile;

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).

share|improve this answer
    
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? –  Johannes Pille Jun 12 '12 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 Jun 12 '12 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 Jun 12 '12 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 Jun 12 '12 at 22:41
    
I've updated the code to reflect your problem description. Is there anything missing from it at this point? –  Andy Adams Jun 15 '12 at 17:59
add comment

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.