WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

WP loops are widely used to print lists of posts in Wordpress:

<?php $loop = new WP_Query( array( 'post_type' => 'ff', 'ff' => 'show_ff', 'orderby' => 'menu_order', 'order' => 'ASC' ));
     while ( $loop->have_posts() ) : $loop->the_post(); ?> 

     <!-- here we're going to have all posts from show_ff category of ff type. -->

     <?php endwhile; ?>  

Is there a way of displaying for example 3 first posts then some element (div in my case) and again 3 next posts?

I know I could do 6 loops in each div, but maybe there's another way of breaking this code? Maybe some if loops inside of while loop? Here's my concept:

    while ( $loop->have_posts() ) : $loop->the_post(); 
     echo '3 first posts';
     echo '<div class="special"></div>';
     echo '3 last posts';
    endwhile; ?>  
share|improve this question
Are you always grabbing 6 posts or will that total amount of posts alternate? – t31os Dec 20 '10 at 12:28
Good question, I don't know how many posts there will be. – Wordpressor Dec 20 '10 at 12:43
up vote 2 down vote accepted

I don't think i've fully grasped how you want this to work, but if i assume for a moment that as long as the loop has at least 6 posts, you want to insert extra markup after the third result in the loop..


Insert extra markup after the third post, when the current iteration has at least 6 posts

if( $loop->have_posts() ) :

    //$post_count = $loop->found_posts; // <-- think this is the total, corrected below
    $post_count = $loop->post_count; // should be the result count for the current page
    $loop_count = 0;

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

        <!-- your regular loop markup, eg. title, content, etc.. -->

        if( $post_count >= 6 && $loop_count == 3 ) :

            <!-- extra markup to put in when it's the end of the third post of this loop -->


This way you avoid the need to use an offset and can using paging if necessary. I havn't tested the code, but i've given numerous examples like this before, simply let me know of any problems and i'll re-examine(and test) the code.

share|improve this answer
Updated the code, should of used post_count(total for the current page) and not found_posts(total posts in the query). – t31os Dec 20 '10 at 13:49
You can use $loop->current_post instead of adding another variable, $loopcount – onetrickpony Dec 21 '10 at 2:20
For the sake of a variable with a numeric counter, it really would make little to no difference at all, what advantage would it offer here, if any? – t31os Dec 21 '10 at 10:36
..less code? :) – onetrickpony Dec 21 '10 at 16:10
Would that be helpful in this context, the code is already pretty slim(it's a basic counter) and would trimming it further make the code easier to understand or offer a measurable difference in run time? I personally don't think it would..(imo, the standard counter is easier for non-techies to understand). – t31os Dec 21 '10 at 16:44

You can limit the number of posts to three in the first loop:

 <?php $args = array('showposts' => 3); 
$the_query = new WP_Query($args); 
while ($the_query->have_posts()) : $the_query->the_post();

Then show your div and then show again 3 posts with the 'offset' parameter to exlude the 3 firsts ones:

 $args2 = array('showposts' => 3, 'offset' => 3 );
 $the_query = new WP_Query($args2); 
while ($the_query->have_posts()) : $the_query->the_post(); 

More infos in the codex.

share|improve this answer
I know it, but what when I want different class for each post? 6 loops? What if there will be 197 posts? 197 loops? – Wordpressor Dec 20 '10 at 12:48
use this then: <div class="post" id="post-<?php the_ID(); ?>">, it'll give a specific ID for each post. – kevin Dec 20 '10 at 12:51
There's really no need to be using an offset, you require only a basic counter.. (if you use an offset you can't use the native paging).. i'll add an answer shortly.. – t31os Dec 20 '10 at 13:07

Your Answer


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.