0

Is it possible to change the layout of the query posts depending on how many there are?

So simple query page listing posts...

Up to 4 posts show them in a list.

If there are 4 posts and over show them as float blocks for example.

1 Answer 1

1

for a standard query, you can check the amount of posts with:

all posts for the query: $wp_query->found_posts

and the posts on the page: $wp_query->post_count

use that with a conditional statement to switch to the different layouts.

example:

<?php if ( have_posts() ) : //start of the loop//
  if ( $wp_query->found_posts <= 4 ) { ?>
     list output of posts 
      //this will need to include the 'while(have_posts())' including the 'endwhile' of the loop
  <?php } else { ?>
     float block output
     //this will need to include the 'while(have_posts())' including the 'endwhile' of the loop
     <?php } 
endif; ?>
7
  • Thank you Michael, i'm unsure how to attach them to a normal query and subsequent conditional statement though..
    – user4630
    Apr 17, 2013 at 10:09
  • 1
    Show us your query
    – Kayo
    Apr 17, 2013 at 10:42
  • I have added an example structure
    – Michael
    Apr 17, 2013 at 11:31
  • Thank you Michael, i've tried your code here.. pastie.org/7629448 and it doesn't work it just shows the second half whether its 2 posts or 10...
    – user4630
    Apr 17, 2013 at 12:33
  • what theme are you using? what it the output if you add echo $wp_query->found_posts; before the code
    – Michael
    Apr 17, 2013 at 13:41

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.