0

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

3
  • 1
    I think @milo has answer, but one thing to mention - query_post() is Bad practice, Use pre_get_post filter instead.
    – Amit Kosti
    Commented Aug 12, 2012 at 16:56
  • 1
    Try using "offset" - see my answer below.
    – Ciprian
    Commented Aug 12, 2012 at 19:23
  • Please go back and work on your older questions too!
    – kaiser
    Commented Aug 13, 2012 at 9:43

3 Answers 3

0

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.

5
  • 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
    Commented Aug 12, 2012 at 20:20
  • Why the negative vote? There are more ways to separate 2 queries and this is one of them.
    – Ciprian
    Commented Aug 26, 2013 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
    Commented Sep 12, 2013 at 10:54
  • This method/answer is by far inferior to the others as it requires you to do two separate queries. There is no need for that - Just count the index and apply the featured_template to count<=2. See Milo's or Syed Balkhi's answers...
    – Lulu
    Commented Feb 19, 2015 at 14:12
  • @Ciprian how would you break the loop to add an advertisement between a certain post number.
    – user32447
    Commented Aug 5, 2015 at 20:50
5

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; 
?>
7
  • Thanks @Milo tried your code but it didn't work, it gave me an error.
    – gil hamer
    Commented Aug 12, 2012 at 20:18
  • 1
    @gilhamer check for syntax error, coz this code is correct.
    – Amit Kosti
    Commented Aug 12, 2012 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
    Commented Aug 12, 2012 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 Kosti
    Commented Aug 12, 2012 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
    Commented Aug 13, 2012 at 9:14
1

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();
?>
3
  • 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
    Commented Aug 12, 2012 at 20:19
  • You can count posts directly in the query using $wp_query->current_post +1;
    – Wyck
    Commented Aug 13, 2012 at 3:40
  • can you please edit your code so I know where exactly to put it? thanks!
    – gil hamer
    Commented Aug 13, 2012 at 7:49

Your Answer

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

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