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 :)

19 Responses to “Wordpress: Make home page show latest post (in the “single post” format)”

  1. Danny Says:

    So, I put this before the loop begins, and it works for me.

    <?php $wp_query->is_single = true; ?>

  2. lucas Says:

    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;

  3. Jennifer Says:

    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!) :D

    Thanks!

  4. lucas Says:

    I am teh looze! :)

  5. Helmet Says:

    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!

  6. PacoKiLi Says:

    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 ;)

  7. Grab the Latest Post from Multiple WordPress Blogs at Elliot Lee Says:

    [...] another good resource on the topic at scriptygoddess, though I didn’t take the time to fully understand what’s going on in that [...]

  8. Justin Says:

    Exactly what I needed… Thanks a bunch!

  9. Nick Prignano Says:

    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.

  10. Brian Says:

    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?

  11. Jim Mirkalami Says:

    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

  12. Cristian Eslava Says:

    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

  13. Dave Says:

    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?

  14. Jennifer Says:

    It should work - there were a couple of versions floating around - which one did you try?

  15. Dylan Says:

    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!

  16. Mchl Says:

    What if you want a index.php to call a specific post (not most recent post) in single post format (comments, comment form)

  17. I Still Love You » Blog Archive » Calling Mr Jane Says:

    [...] Jamis @ Just Good Clean Fun talks about how to add five wordpress posts on an external html page. [...]

  18. jgoode Says:

    thanks so much, this is exactly a piece of info i was missing to make the comments do what i wanted. thanks for sharing!

  19. Strange Says:

    I have is_single = true works and it works…. but it breaks the navigation on the homepage.

    How can I get these functions to work at the same time?

    next_posts_link()
    previous_posts_link()

    Thanks for your help

Leave a Reply

Subscribe without commenting