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

I am trying to create a custom homepage where the site content is shown as follows...

See image...


With separate loops this is simple... 3 individual query_posts inside a category fetch loop...

But I want to reduce the number of query posts from 3 to 1 per category since they are coming from the same category...

Is this possible... (since I couldn't figure out how to wrap 3 iterations with a div class block)

share|improve this question
up vote 0 down vote accepted

You can achieve this by checking where you are in the loop and outputting markup at the appropriate time. Within the object that holds the query results is a counter that keeps track of the current post, $wp_query->current_post. Note that it is zero-indexed, so first is 0, 2nd is 1, etc..

while( have_posts() ):

    if( $wp_query->current_post == 0 ):

        // this is the first post
            <div class="one">
                <?php the_title(); ?>
            <div class="two">

    elseif( $wp_query->current_post > 0 && $wp_query->current_post < 4 ):

        // this is the second - fourth posts

    elseif( $wp_query->current_post == 3 ):

        // this is the fourth post
            <!-- close div class=two -->


share|improve this answer
I ran the code... But I don't know why this check is not validating elseif( $wp_query->current_post == 3 ): The output is <div class="one"> Hello world! </div> <div class="two"> Another Post with Everything In It An Ordered List Post A Simple Text Post – Debajyoti Das Jun 22 '12 at 10:25
Ya its working fine.... after changing it to elseif( $wp_query->current_post == 4 ): – Debajyoti Das Jun 22 '12 at 10:51

Try like this:

query_posts( 'posts_per_page=9' );
$postcount = 1;
$firsttop = false;
$firstbottom = false;
$secondtop = false;
$secondbottom = false;
$thirdtop = false;
$thirdbottom = false;
while ( have_posts() ) : the_post();
if($postcount = 1){
//open first div...
$firsttop = true;
//call first post structure...
//close first div...
$firstbottom = true;
else if($postcount > 1 and $postcount < 5){
//open second div...
$secondtop = true;
//call first post structure...
//close second div...
$secondbottom = true;
else {
//open third div...
$thirdtop = true;
//call third post structure...
//close third div...
$thirdbottom = true;

// Reset Query
share|improve this answer

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.