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
Related posts:
- Striping IMG tags from the_content in WordPress (and how to fudge page excerpts) Background: For a site I was working on, I was...
- Search from WordPress Admin (Post Listing) Redirecting to Home Page This is possibly the most bizarre thing I've ever seen....
- Adding a block of HTML to a WordPress post One problem with the WYSIWYG editor in WordPress is that...
Related posts brought to you by Yet Another Related Posts Plugin.
scriptygoddess
