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.

OK so I've searched a lot and couldn't find a straight forward answer to my question. I am simply trying to use one category loop that will display posts in a category page in a wordpress site. lets say 6 posts for this purpose.

it's pretty simple to accomplish it by using a simple query loop like so:

<?php query_posts('showposts=6'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
// content or anything comes here
<?php endwhile; endif; ?>

however, I am trying to "break" my loop into 2 different layouts:

  1. 2 posts showing with featured image
  2. the rest of the 4 in a different design which will be just showing title + excerpt + read more

Here is a sample which best demonstrates what I am trying to accomplish: https://dl.dropbox.com/u/4405634/split-loop-layout.jpg

There are tons of solutions in the web, however some of the are too complicated and some don't work in a category loop.

I have seen it in a lot of websites and still can't find a simple way of doing it. does someone have a nice solution?

Thanks a lot. Gil

share|improve this question
1  
I think @milo has answer, but one thing to mention - query_post() is Bad practice, Use pre_get_post filter instead. –  amit Aug 12 '12 at 16:56
1  
Try using "offset" - see my answer below. –  Ciprian Popescu Aug 12 '12 at 19:23
    
Please go back and work on your older questions too! –  kaiser Aug 13 '12 at 9:43
add comment

3 Answers

up vote 0 down vote accepted

Just create 2 queries using the "offset" parameter and separate them by:

<?php wp_reset_query(); ?>

Display posts from the 4th one (offset is starting from 0):

<?php $query = new WP_Query( 'offset=3' ) ); ?>

EDIT: Here's some sample code:

<?php query_posts('offset=0&showposts=2&order=ASC'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endwhile; endif; ?>

<?php wp_reset_query(); ?>

<?php query_posts('offset=2&showposts=4&order=ASC'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endwhile; endif; ?>

That's it. You'll have 2 posts which you can style any way you want, and the next four, which, again, you can style differently.

share|improve this answer
    
can you write a sample of a working code? I am not that coder and not sure where to start. it will be great! thanks! –  gil hamer Aug 12 '12 at 20:20
    
Why the negative vote? There are more ways to separate 2 queries and this is one of them. –  Ciprian Popescu Aug 26 '13 at 13:48
    
Dont really remember why. it was long time ago when I rated it. anyway - it didn't work for me. I found another solution. changed your rating to positive. –  gil hamer Sep 12 '13 at 10:54
add comment

check which post you're currently outputting via $wp_query->current_post:

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

                // first two post to show both title, excerpt
                if( 2 > $wp_query->current_post ) :
                    the_title();
                    the_excerpt();

                //else (remaining) show only title    
                else :
                    the_title();
                endif;

        endwhile;
    endif; 
?>
share|improve this answer
    
Thanks @Milo tried your code but it didn't work, it gave me an error. –  gil hamer Aug 12 '12 at 20:18
1  
@gilhamer check for syntax error, coz this code is correct. –  amit Aug 12 '12 at 21:40
    
Maybe I am doing something wrong. can you tell me how do I put the_title in the first post and the_excerpt in the second one? thanks –  gil hamer Aug 12 '12 at 21:42
    
put the_title(); after if( 2 > $wp_query->current_post ) : to print it for first 2 posts and the_excerpt(); after else : to show on remaining posts. –  amit Aug 12 '12 at 22:26
    
Hi Amit, your code really works, however, I have so much extra HTML to put in between and in this way of coding it makes it very complicated since you need to deal with echoing a lot of stuff and a lot of single and regular quotation marks. I will try anyway, thanks a lot –  gil hamer Aug 13 '12 at 9:14
show 2 more comments

You can do this fairly easily by using a counter in your loop like this:

<?php
$counter = 1;
$myquery = new WP_Query( array( 'posts_per_page' => 6 ) );
while ( $myquery->have_posts() ) : $myquery->the_post(); 

if($counter == 1) { ?>

First post Look Different

<?php } else { ?>

Rest looks different

<?php
}
$counter++;
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
share|improve this answer
    
this was actually working @Syed Balkhi but the problem is that it shows all posts regardless if I am in a certain category. I would like to get the first 6 posts but according to a certain category I am in. is it possible? –  gil hamer Aug 12 '12 at 20:19
    
You can count posts directly in the query using $wp_query->current_post +1; –  Wyck Aug 13 '12 at 3:40
    
can you please edit your code so I know where exactly to put it? thanks! –  gil hamer Aug 13 '12 at 7:49
add comment

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.