scriptygoddess RSS Feed
 
 
 
 

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

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

  1. 1
    Danny:

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

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

  2. 2
    lucas:

    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. 3
    Jennifer:

    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. 4
    lucas:

    I am teh looze! :)

  5. 5
    Helmet:

    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. 6
    PacoKiLi:

    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. 7
    Grab the Latest Post from Multiple WordPress Blogs at Elliot Lee:

    [...] 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. 8
    Justin:

    Exactly what I needed… Thanks a bunch!

  9. 9
    Nick Prignano:

    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. 10
    Brian:

    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. 11
    Jim Mirkalami:

    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. 12
    Cristian Eslava:

    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. 13
    Dave:

    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. 14
    Jennifer:

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

  15. 15
    Dylan:

    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. 16
    Mchl:

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

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

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

  18. 18
    jgoode:

    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. 19
    Strange:

    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

  20. 20
    lalit:

    visit

    http://mytechnospace.wordpress.com/2009/01/01/to-display-one-post-per-page-in-wordpress/

    for To display one post per page in WordPress

    with no codding

  21. 21
    Jennifer:

    @lalit: I'm pretty sure your method will not display it as a "single" view though (ie with comments). (Althought maybe that's changed now in the latest version of Wordpress. I'll have to test it to be sure…)

  22. 22
    Jason:

    Has anybody figured out how to list posts for the day in an actual post and not making a loop for it?

  23. 23
    Jennifer:

    You'd probably want to use the "get_posts" call instead of the loop and then pass in the current month/day/year using PHP…

    get_posts('monthnum='.date('n').'&day='.date('j').'&year='.date('Y'));

    That's untested but I think it should work. Read more here and here (get_posts now is the same as query_posts fyi)

  24. 24
    Tim:

    To use the solution that Danny first pointed out and only display one post on the home page, throw in a counter and in if statement and that should solve your problem – for complete details check out http://www.silentgap.com/silentgap/2009/display-only-one-single-view-post-in-wordpress/

  25. 25
    David:

    Is there any way to actually _redirect_ to the latest post? These solutions seem only to recreate the single post page experience on the home page, whereas I want the user to be taken to the permalink page. Any ideas? thnx

  26. 26
    Jennifer:

    @David – yes this should do the trick (tested on a local copy of wordpress)
    Make a backup copy of your index.php in your theme folder (just in case) – and then delete EVERYTHING in it and replace with the following:
    <?php
    require('./wp-blog-header.php');
    if (have_posts()) : the_post();
    header("location: ".get_permalink());
    exit;
    endif;
    ?>

  27. 27
    David:

    Thanks, Jennifer! This worked great :)

Leave a Reply

Categories

Archives

Bookmarks

WordPress Resources

Meta

ADVERTISEMENTS