scriptygoddess

28 Apr, 2007

WordPress: Make home page show latest post (in the "single post" format)

Posted by: Jennifer In: WordPress|WordPress Bookmarks|WordPress Hacks|WordPress: Lessons Learned

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

50 Responses to "WordPress: Make home page show latest post (in the "single post" format)"

1 | Danny

April 28th, 2007 at 9:58 am

Avatar

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

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

2 | lucas

April 28th, 2007 at 7:55 pm

Avatar

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

April 28th, 2007 at 10:38 pm

Avatar

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!

4 | lucas

April 29th, 2007 at 2:03 pm

Avatar

I am teh looze! :)

5 | Helmet

July 29th, 2007 at 3:48 am

Avatar

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

July 31st, 2007 at 12:47 pm

Avatar

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

August 29th, 2007 at 6:56 pm

Avatar

[…] 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

October 16th, 2007 at 8:55 am

Avatar

Exactly what I needed… Thanks a bunch!

9 | Nick Prignano

December 2nd, 2007 at 11:30 pm

Avatar

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

January 14th, 2008 at 1:05 am

Avatar

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

February 6th, 2008 at 4:27 pm

Avatar

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

February 23rd, 2008 at 12:36 pm

Avatar

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

February 24th, 2008 at 2:34 pm

Avatar

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

February 24th, 2008 at 4:14 pm

Avatar

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

15 | Dylan

March 10th, 2008 at 5:29 pm

Avatar

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

March 31st, 2008 at 3:33 am

Avatar

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

April 7th, 2008 at 4:14 am

Avatar

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

18 | jgoode

April 18th, 2008 at 11:15 am

Avatar

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

June 27th, 2008 at 9:57 am

Avatar

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

21 | Jennifer

January 1st, 2009 at 10:41 pm

Avatar

@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 | Jason

January 14th, 2009 at 3:42 pm

Avatar

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

23 | Jennifer

January 14th, 2009 at 4:51 pm

Avatar

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 | Tim

February 2nd, 2009 at 4:57 pm

Avatar

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 | David

April 1st, 2009 at 5:40 am

Avatar

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 | Jennifer

April 1st, 2009 at 12:40 pm

Avatar

@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 | David

April 1st, 2009 at 4:03 pm

Avatar

Thanks, Jennifer! This worked great :)

28 | muiiio

July 16th, 2009 at 11:04 am

Avatar

Thank you! I have been wondering how to do that! Namely, the redirect one – I know squat about PHP ๐Ÿ˜› Thank you again!

29 | Bharathi

July 23rd, 2009 at 6:25 am

Avatar

thank u Jennifer.it is working for me.

30 | Yegor

October 1st, 2009 at 6:38 am

Avatar

I need your advise.
I need to post only 3 latest articles from 1 category on the page. How can I do that?
Thanks.

31 | Adrian

October 25th, 2009 at 8:30 pm

Avatar

Old post here, but still valid/useful. Buuuump. I've been Googling and cannot find a solution to my particular problem > while all the codes mentioned here work in WP 2.8 (the index displays only one post), I have one issue: they all break my next_posts_link/previous_post_link. =P

32 | Jennifer

October 25th, 2009 at 10:02 pm

Avatar

Adrian,
I tested the following code on a local copy of wordpress and it worked… (I placed this just below get_header()

global $wp_query;
query_posts(
array_merge(
array('posts_per_page' => 1),
$wp_query->query
)
);
$wp_query->is_single = true;

And the previous_post_link / next_post_link appear to work…

33 | Adrian

October 26th, 2009 at 6:52 am

Avatar

Thanks for responding Jen! I tried your code, and it will allow one post on the index, but it still breaks those next/previous links. Of course, if I have the Settings > Reading > Blog pages show at most: 1 post – the links work fine on the main page. But, then my archives.php page does the same thing – it displays only one post from the category selected and displays the next/previous links. What I'm wondering is if this can be bypassed by passing a different code on archives.php so that it will display all posts – rather than just one.

Anyway, I'm also digging through WP support forum threads and will post here if I find a solution.

Thanks again;

34 | Jennifer

October 26th, 2009 at 7:52 am

Avatar

Are you using next_POSTS_link or next_post_link? (if you're using next_POSTS – try using next_POST…)

Also – no need to change the settings to 1post. It should still work.

35 | Adrian

October 26th, 2009 at 8:25 am

Avatar

I was using POSTS. I changed it to POST and the text for the link does appear 'Newer Article' – but it isn't a link. Understandably I suppose, and the newest post is already displayed. There are older articles, but no link or text 'Older Articles' appears. Here is what I have:

1),
$wp_query->query
)
);
$wp_query->is_single = true;
?>

And

Thanks;

36 | Adrian

October 26th, 2009 at 8:28 am

Avatar

Yikes. Let me try again:

query_posts(
array_merge(
array('posts_per_page' => 1),
$wp_query->query
)
);
$wp_query->is_single = true;

AND

next_post_link('« Older Article')
previous_post_link('Newer Article »')

Thanks;

37 | Jennifer

October 26th, 2009 at 9:01 am

Avatar

next_post_link and previous_post_link work a little differently than next_POSTS I believe… (so you just need to switch your text around ie. next_post = newer article, previous_post – older article…

as far as it not being a link – that's weird! Is next_post, etc within the loop? (it should be)

38 | Jennifer

October 26th, 2009 at 9:17 am

Avatar

Sorry – my bad – now I see the problem. You need to look up how next_post_link() / previous_post_link() is supposed to be used…

http://codex.wordpress.org/Template_Tags/next_post_link

so what you need is this:

previous_post_link('« %link', 'Older Article')
next_post_link('%link »','Newer Article')

39 | Adrian

October 26th, 2009 at 9:54 am

Avatar

Pow! Working! Thanks;

40 | Jennifer

October 26th, 2009 at 10:02 am

Avatar

Hey – you know I just tested this again. I think as long as you're using next_post_link and previous_post_link (and using it with the right format) – all you need is the:
$wp_query->is_single = true;
(forget the additional code I added… query_posts and array merge junk – that was for something else I was working on) ๐Ÿ˜€

41 | Adrian

November 2nd, 2009 at 10:55 pm

Avatar

Thanks Jen. Just dropped a tip in your jar. 8)

42 | Kushal Bhatt

January 6th, 2010 at 10:59 pm

Avatar

Thanks for this post! I was doing the same thing today and used this solution : )

43 | John Fox

February 8th, 2010 at 5:23 pm

Avatar

@Jennifer Post #26 : many thanks for your post. I was spinning my wheels most of the day today until I found your post.

One note is that on my server I had to use the full path to my wordpress directory (WP is not in my root).

So, my require was something like:
require ('home/sitename/public_html/wp/wp-blog-header.php');

44 | Jon White

April 12th, 2010 at 1:08 pm

Avatar

I know it's a few years old by now, but I have to thank you *so* much for this post. :) It really was the final mystery to solve before going live with my newest site, which relies completely on the Index-behaving-like-a-Single to work. (Especially with that "wait I didn't actually think this through" problem of the "PREVIOUS POST" link sitting right there on the home page, despite having looked so inconsequential in Photoshop…) Here's the finished product in the wild:

http://todaysdocument.com

Thanks again!

45 | Heylen

July 15th, 2010 at 2:32 pm

Avatar

Wow thanks! I was looking for this!

46 | Mark Petherbridge

August 25th, 2010 at 5:06 pm

Avatar

If you need to know how to display the most recent post on an index page that is separate to the WP page ive just wrote this article:

http://bit.ly/9MDCcj

47 | BJ

September 1st, 2010 at 4:07 pm

Avatar

@ post 26 to Jennifer.

This is so sexy!

Girls that know their business…
I've been searching for this for 2, almost 3 days and it can be so easy. Just a little bit of code.
I almost killed the WordPress team!!!

You are my Hero for this month..
BJ

48 | heerko

February 28th, 2011 at 7:49 am

Avatar

I know this is an old thread, but I bumped into it today and I thought i'd share my solution.

Put this in home.php:

Works for me, displaying the latest post. But the site I am making is extremely simple with just one category, so YMMV.

H

49 | heerko

February 28th, 2011 at 7:51 am

Avatar

Oops. The php tags got stripped, this is the code:

include ('single.php'); die();

50 | Storyleader

May 31st, 2011 at 7:54 am

Avatar

I searched all over for a solution. Here is what worked for me, using WordPress 3.13:

1. In my theme, I had no "home.php." So I copied "single.php" in my theme and renamed it "home.php".

At this point, visitors to my site saw a front page containing all my my posts, each displayed in full, including comments.

2. I installed the plugin "Custom Post Limits": http://wordpress.org/extend/plugins/custom-post-limits/ (it said it hadn't been tested past 2.6, but it works fine for me).

3. In the Custom Post Limits setting, I changed the value for "Front Page Limit" to "1".

Bingo! Visitors to http://www.storydynamics.com/stories see the latest newsletter in its full form, including comments.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements