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.
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
Isn’t this a simple setting in the WordPress admin backend?
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.
Just wanted to say that I tried this out and your line…
endwhile;endif; ?>
should be
other than that thanks for this quick solution!
I have much simpler than everyone
just visit —-
http://mytechnospace.wordpress.com/2009/01/01/to-display-one-post-per-page-in-wordpress/
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
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.
OK, so this goes in the index.php file of somehwere else? (This question is for all the newbs - like me - out there)!!
Trackbacks: