0

I am trying to make a so called featured category on my theme homepage. In that category I would like to display 6 posts from same category. The problem is that, I would like to devide these posts into three different html blocks. For example:

<div>first 2 posts goes here</div>
<div>second 2 posts goes here</div>
<div>last 2 posts goes here</div>

The three divs have different html structure (eg. post image size etc). So, does it mean that I have to run 3 separate queries to get the above posts from the same category? Or would that be too much of load and I should use only one post query? Thanks.

1

Just add a counter to the loop.

Just use $wp_query->current_post

while ( have_posts() ) : the_post();
    if($wp_query->current_post <= 1) // 1 because counter starts at 0
        // <div>first 2 posts go here</div>

    if($wp_query->current_post > 1 <= 3)
        // <div>second 2 posts go here</div>

    if($wp_query->current_post > 3 <= 5)
        // <div>last 2 posts go here</div>
endwhile;
  • You don't need a separate counter, wordpress has one already at $wp_query->current_post – Mridul Aggarwal Oct 26 '12 at 10:03
  • ah, didn't know that. Thx, I've edited the answer. – xsonic Oct 26 '12 at 10:36
0

You don't need multiple queries. You don't even need to put them all in a loop. For your example

while(have_posts()) :
    the_post();

    //display post

    if(!have_posts())
        break;

    the_post();

    //display post
endwhile;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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