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.
15 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)!!
This is amazing. I’ve been looking around for a solution to set my home page to show only one post. Other solutions seem to disable to “next page” “previous page” function… but this one works like charm.
Thanks for sharing Man… bless you
Thank you!
I’d need some advice how to integrate this code.
I assume this code is placed into the index.php of my theme.
But where? If I try to paste it, I get an error output.
Desperatly searching for an anwer.
Thx in advance.
The third line looks like a problem to me. Shouldn’t it be “if ($page_num=”) $page_num =1;”?
Well I found out where to place it so it works.
Somehow it works in both ways ..
if ($page_num=”) $page_num =1;
query_posts(‘showposts=7&paged=’.$page_num); ?>
// WordPress loop
endwhile;endif; ?>
- doesn’t work (endwhile;endif; ?> gets posted as text after the actual post
if ($page_num=”) $page_num =1;
query_posts(‘showposts=7&paged=’.$page_num); ?>
- works
if ($pagenum=”) $pagenum =1;
query_posts(‘showposts=7&paged=’.$page_num); ?>
- works
If i try to achieve what I need, only this version will work I think (page 1 less posts than the following pages):
if ($page_num=”) $page_num =1;
But I really don’t get how I could get anything out of this line to alter my posts per page on different page id’s. Basically I’m not into php
Any idea?
best regards
Trackbacks: