Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

See image...

http://i.stack.imgur.com/ykrdf.png

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

2 Answers 2

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

<?php
while( have_posts() ):
    the_post();

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

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

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

        // this is the second - fourth posts
        the_title();

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

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

    endif;

endwhile;
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){
if(!$firsttop){
//open first div...
$firsttop = true;
}
//call first post structure...
$postcount++;
if(!$firstbottom){
//close first div...
$firstbottom = true;
}
}
else if($postcount > 1 and $postcount < 5){
if(!$secondtop){
//open second div...
$secondtop = true;
}
//call first post structure...
$postcount++;
if(!$secondbottom){
//close second div...
$secondbottom = true;
}
}
else {
if(!$thirdtop){
//open third div...
$thirdtop = true;
}
//call third post structure...
$postcount++;
if(!$thirdbottom){
//close third div...
$thirdbottom = true;
}
}
endwhile;

// Reset Query
wp_reset_query();
share|improve this answer

Your Answer

 
discard

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.