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.. Commented Apr 17, 2013 at 10:09
  • 1
    Show us your query Commented Apr 17, 2013 at 10:42
  • I have added an example structure Commented 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... Commented 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 Commented Apr 17, 2013 at 13:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.