Make WP show only one post on the front page
Someone on the WP support forum asked if it was possible to only show a single post on the front page. Thanks to the query_posts, and the template hierachy, this can be done fairly easily without resorting to any plugins.
First you'll need to make a copy of the index.php file in your theme directory. Rename this file home.php. This file will take precendence over index.php and be used for the front page display.
Now, at the top of this file, you'll need to make a call to query_posts and utilise one of the parameters I had previously blogged about.
<?phpget_header();query_posts('posts_per_page=1'); //returns only the front page?>
We're using the posts_per_page parameter to request that only a single post is retrieved and populated into the query object for The_Loop.
So why not change the reading options in the WP Admin panel? The reason is because this will also affect the pagination for searches and archives. Query_posts offers a simple, elegent solution to this problem.
Update: Some themes still use the old WP 1.2 version of the_loop e.g.
<?php if ( $posts ) : foreach ( $posts as $post ) : start_wp(); ?>
instead of the WP 1.5
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
These will need to be modified to the 1.5 style before this technique can work.
-30-
Finally, an answer that is clear enough for me to try.
I’ve been trolling around the WP forums, and there are about 50 different (and mostly complex) ways of putting a single post on a home page, combined with some static content.
Basically, I’m using WP as a CMS, and the blog as a high powered news section.
Anyway, thanks for making this nice and clear, for us PHP impaired.
[…] If you want only one post to show up on the front of your WordPress powered blog, then If..Else Log has the answer.
[…]
How do you achieve one post on the main page and yet have the remaining posts show up as linked excerpts (like the ifelse page).
Thanks, this is very handy and worked like a charm!
Nice tip, however I would like to know if there is a way to show the full post (full content), beacuse with your tip, the_content() is cut-off by the “more” tag.
I have been looking for everywhere but have not found anything.
Thanks!
[…] under Tips, CMS | permalink/trackback | no comments » social bookmarking - del.icio.us, my web 2.0,digg […]
It seems that this breaks
posts_nav_link(). Using home.php and eitherquery_posts('posts_per_page=x')query_posts('showposts=x')for x any integer, results in links which give
http://www.erectlocution.com/boxing/page/y
for y some integer page number but with the same content as on the front page.
Any thoughts?
That’s exactly what I’ve been looking for! The hack works like a charm. Thanks!
~Jonathan
Seems like this makes the Previous | Next links below the post not work.
Any ideas?
If you’re using a theme that uses the old WP 1.2 version of The Loop, how do you modify it to the WP 1.5 version?
[…] 1) For Wordpress users with themes using the 1.5 version the Loop, you can find help here on how to create a home.php and make a call to query posts. Very easy to follow. […]
Hmm…
I show one post on the front page the follwing way:
It is some way incorrect?
Does anyone know how to always display the entire content of the latest entry (no matter how long it is, and no matter if it’s got a more-tag or not), and then excerpts of the following entries?
Unfortunately it does break the prev/next links. I have been searching for a solution for this but not found it yet. I’m afraid the only way to fix it is creating a hack.
And would someone care to share such a hack with us? I would like these links to work on my page as well, without having to alter the “Reading” set in the WP admin.
Nice man, this rocks. So easy and elegant. Thanks.
I did exactly as you write. But there’s no full post content on my blog home page — only the part with ‘more’ link. Hmm… bug in WP or did I do anything wrong?
With regard to prev/next links, what mechanism are they using?
I too would like a single post for a particular category on my home page, but with the ability to scroll to previous/next posts as on a single page.
If I knew what code prev/next links were employing I could perhaps write a hack - unless someone already has one they’re prepared to share.
Ooops, a bit abstract in my last post.
By “If I knew what code prev/next links were employing…” I mean functions and classes and which file(s) they sit in.
Where do I place the code in home.php?
Am I missing some of the basic instructions?
Yo! I guess it was as easy as that. I posted earlier about needing more info, and of course as soon as I did I applied the code and of no problems. I should have learned by now that I should try before why!
Cheers,
Mjo
[…] After reading through the posts at if…else postquery redux, queryposts, and One Post On Front Page I was able to manipulate wordpress theme into a blog and photoblog theme like I’ve always wanted for IU. […]
HI,
I think your code is what I am looking for, but under the first post I would like to show links to previous posts - date, title and maybe number of comments. Then people click on the link and it will load up the page for that post.
Is this possible please?
[…] Make WP show only one post on the front page […]
[…] IfElse posted a solution for showing only one post but this breaks the next/previous links at the top. I have spent ages trying to find a fix for this but nothing so far. :( […]
It works, but it breaks the next/prev links. Another solution ?
My theme included the wp1.2 loop, and when I try to copy over the 1.5 loop it just breaks, so I’ll start with the basic questions;
- Is it just a matter of changing over the home.php over to the wp1.5 loop, or do all of the template that use the loop have to be changed over?
- Am I right in presuming that I only need to change over the php script that has been included above, i.e. replace
with this
and the fix works?
This looked like a great solution until it broke the prev & next permalink navigation. I see others have had this same problem without getting a fix.
Here is a plugin that I found to be even better because you can control the number of posts on more than just the home page.
http://wordpress.org/support/topic/82794?replies=2#post-424535
Hope this helps!
-Brad
[…] Thanks ifelse for the tip, which I now pass on to you. My photoblog theme by brajeshwar is great and does exactly what I wanted which was to show one image on the front page, but only if you set your options>reading>show at most setting to 1. This was fine but I found it restricted the category and archive pages unless you had the Custom Query String plugin installed and activated. […]
Thanks for posting this tip. I was having trouble where setting the number of posts to show on the front page was limiting the number of posts shown in the archives too. This fixed it.
It works!
100%, it works!
Thank you soo much!
I’ve read countless ways on how to change the number of posts for your home page. They involved plugins and other softwares. But you have successful explained it in a simple and exquisite way. I thank you.
How do you get this to show a specfic post… eg the Welcome message rather than the latest post? Nice idea anyway.
I found the solution to the broken navigation links in one of the WordPress forums (didn’t think to save the URL, sorry!). You have to add the “paged” parameter to your query, like this:
<?php if (is_home()) {$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("showposts=1&paged=$page");
} ?>