1

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; ?>  
2
  • Are you always grabbing 6 posts or will that total amount of posts alternate?
    – t31os
    Dec 20, 2010 at 12:28
  • Good question, I don't know how many posts there will be. Dec 20, 2010 at 12:43

2 Answers 2

2

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

Example

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

<?php
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(); 
        $loop_count++;
        ?>

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

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

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

            <?php
        endif;
    endwhile;
endif;
?>

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.

5
  • 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, 2010 at 13:49
  • 1
    You can use $loop->current_post instead of adding another variable, $loopcount Dec 21, 2010 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, 2010 at 10:36
  • ..less code? :) Dec 21, 2010 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, 2010 at 16:44
0

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.

3
  • I know it, but what when I want different class for each post? 6 loops? What if there will be 197 posts? 197 loops? Dec 20, 2010 at 12:48
  • 1
    use this then: <div class="post" id="post-<?php the_ID(); ?>">, it'll give a specific ID for each post.
    – kevin
    Dec 20, 2010 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, 2010 at 13:07

Your Answer

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

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