How to define how many post to display per page

By default, the WordPress loops displays 10 posts per page. But in case you’d like to display more or less posts, here’s how to achieve it.

The solution to that common issue is pretty simple: You have to use the query_posts() function, which gives you a total control over the WordPress loop.

To specify how many posts you want to be displayed per page, we'll use the showposts parameter.

Here's an exemple:

<?php
$page_num = $paged;
if ($pagenum='') $pagenum =1;
query_posts('showposts=7&paged='.$page_num); ?>
               	<?php if (have_posts()) : ?>
		<?php while (have_posts()) : the_post(); ?>
                // WordPress loop
                endwhile;endif; ?>

This exemple loop will displays 7 posts per page. This is the code I use on WpRecipes.
Did you noticied the paged='.$page_num parameter? This allows use to display our content by pages and use a paginator.

9 Responses

Oct 11 2008 10:46

Isn’t this a simple setting in the WordPress admin backend?

Oct 11 2008 11:01

Sure, it can be defined under Settings → Reading. But this recipe should still be useful: For exemple, you may want to have two (or even more) distinct loops on your homepage.

Dec 18 2008 13:14

Just wanted to say that I tried this out and your line…
endwhile;endif; ?>

should be

other than that thanks for this quick solution!

Jan 04 2009 11:59
Feb 08 2009 17:01

I think people are missing the point of this… With this type of code you can change the number of posts per page in different templates ie; main page shows 4 posts, archives shows 20. You dig? Nice code, thanks. Rob

PS: I think this is directed at people who are editing their code, not .org users ;)

Feb 20 2009 17:25

To show one post on my front page I edited this showposts to 1 on my index.php file. Now it is working. Thanks for your help.

Feb 26 2009 10:48

OK, so this goes in the index.php file of somehwere else? (This question is for all the newbs - like me - out there)!!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required
Blog And Make Cash