Change content off every sixth element

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.

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

Attribution
Source : Link , Question Author : jmysona , Answer Author : Andy Adams

Leave a Comment