Wordpress: Make home page show latest post (in the “single post” format)
In a custom wordpress theme design I was working on tonight, I was trying to do as the title describes - make the home page show only one post (yes I know there’s already a built in way to do this) but I wanted it to show up the same way the single view shows it - with the comments expanded and the comments form visible, etc. If you know of another/better way to do this, please say so in the comments. But this DID do the trick. I found this page in the support archives. The idea was to create your index.php to look like you wanted (ie. exactly like the “single.php” page) but add this before you start the loop:
<?php
$i=0; // Initialize to Zero;
if (have_posts()) :
while (have_posts()) : the_post();
if ($i==0) {$recentpostid = $post->ID; $i=$i+1;}
endwhile;
endif;
//get only the latest post
$posts = query_posts( 'p='.$recentpostid."'");
?>
and then you start your loop like you normally would…
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Worked for me!
Update As Danny pointed out in the comments - there’s a simpler way to do this. Put the following code on the page (I put it before the loop and it worked)
<?php $wp_query->is_single = true; ?>
Much simpler ![]()
April 28th, 2007 at 9:58 am
So, I put this before the loop begins, and it works for me.
<?php $wp_query->is_single = true; ?>
April 28th, 2007 at 7:55 pm
or….
<?php$x=TRUE;
if (have_posts()) : while (have_posts() && $x) : the_post();
$x=FALSE;
?>
this’ll do it a little more elegantly, IMO.
you can also do this:
<?php$i=0;//standard counter
$max=1;//maximum posts to output
if (have_posts()) : while (have_posts() && $i) : the_post();
?>
That will allow you to limit the amount of posts to any arbitrary number.
That is if you have Wordpress able to show 40 posts per page and you only want to show 5 you’d set $max=5;
April 28th, 2007 at 10:38 pm
Lucas - I just tried your suggestion - and while that does make only one post appear - it doesn’t show up as a “single” view of the page (ie with comments and comments form).
But, Danny - your suggestion does work (and is a lot cleaner than the original version!)
Thanks!
April 29th, 2007 at 2:03 pm
I am teh looze!
July 29th, 2007 at 3:48 am
Jennifer,
I prefer your solution, though. As I use the_excerpt_reloaded function none of the cleaner, simpler ways worked. Seems like the plugin stops working when messing with the loop;)
Thanks!
July 31st, 2007 at 12:47 pm
This is awesome!
Thanks Goddess!!! You’re amazing!!!
I spend all the morning finding this, thanks a lot for this code. I love it, and I love you
August 29th, 2007 at 6:56 pm
[...] another good resource on the topic at scriptygoddess, though I didn’t take the time to fully understand what’s going on in that [...]
October 16th, 2007 at 8:55 am
Exactly what I needed… Thanks a bunch!
December 2nd, 2007 at 11:30 pm
I used the first snippet Lucas posted and it worked beautifully for what I wanted, not showing the comment form and displaying the pagination function. Thought I’d let you know in case you wanted to update the article. Thanks guys.
January 14th, 2008 at 1:05 am
I have a problem with both scripts:
With the short, easy one, it displays multiple posts on days with multiple posts - and it affects the them layout (only the background color in mine).
With the longer one, it affects my other pages - none of them appear.
Anyone else experiencing issues?
February 6th, 2008 at 4:27 pm
I have been a frequent visitor of this blog for some time now, so I thought it would be a good idea to leave you with my thanks.
Regards,
Jim Mirkalami
February 23rd, 2008 at 12:36 pm
I found an easier way if you set this at any page (index, archive…)
and then you put within the loop
It works just fine for me
February 24th, 2008 at 2:34 pm
I may be wrong, but the example here doesn’t do as in the original post.
I want my index.php page to show the most recent post with all comments visible and the add comment form appearing.
Does anyone know how I can do this?
February 24th, 2008 at 4:14 pm
It should work - there were a couple of versions floating around - which one did you try?
March 10th, 2008 at 5:29 pm
So, I put this before the loop begins, and it works for me.
Danny, I owe you a beer.
You just ended 24 hours of stress. Thank you so much!
March 31st, 2008 at 3:33 am
What if you want a index.php to call a specific post (not most recent post) in single post format (comments, comment form)
April 7th, 2008 at 4:14 am
[...] Jamis @ Just Good Clean Fun talks about how to add five wordpress posts on an external html page. [...]
April 18th, 2008 at 11:15 am
thanks so much, this is exactly a piece of info i was missing to make the comments do what i wanted. thanks for sharing!