I just created a simple WordPress installation. It was only going to be short pages and a simple shopping cart, but at the last minute, they decided to add a blog. Since the area for text was so small, we decided to only have one post for every page. As it would only act as duplicate content, there is no point in using the blog home page at all.
With some help from WP Recipes, I created this code to redirect the blog home page (home.php template in the WordPress Theme Folder) to the first post:
<?php
/*
home.php (Blog Home Page):
Redirect To First Post
*/
if (have_posts()) {
while (have_posts()) {
the_post();
wp_redirect(get_permalink());
}
}
?>
It’s actually very simple. It just uses the WordPress Redirect function to redirect to the first permalink that would show up on that page.
Enjoy,
Ashton Sanders
14 Comments
I am working backwards; suddenly my blog is redirecting to the first post instead of to the blog homepage. I’m checking the htaccess file and soon as these files stop hiding! Thanks for the code and post.
[…] to Ashton Sanders for the […]
[…] { while (have_posts()) { the_post(); wp_redirect(get_permalink()); } } ?> Thanks to Ashton Sanders for the tip!GD Star Ratingloading…Share this Related posts:How to set a maximum word count on […]
Useful snippet, thanks!
[…] on the code.That’s the way to redirect your visitors to latest post instead of homepage.Via: Ashton SandersYou Might Like Theses Posts:Make WordPress 404 Error Not Found Page Visitor FriendlyAdd Breadcrumbs […]
[…] on this topic.Today Tricks is How To Redirect Blog to First Post. This code original writen by Ashton Sanders. You need to put this code on your index.php or home.php to make it work. It just uses the […]
[…] { while (have_posts()) { the_post(); wp_redirect(get_permalink()); } } ?> Thanks to Ashton Sanders for the tip!VN:F [1.9.7_1111]please wait…Rating: 0.0/5 (0 votes cast)Share this Related posts:How […]
[…] wp_redirect(get_permalink()); } } ?>Autorem tohoto krátkého tipu je Ashton SandersMiroslav Ratesman KnedlaPodobné ?lánky:WordPress rady pro pokro?ilé – jak nastavit funkci […]
[…] to New Article on WordPressby Haitamy – September 28th, 2011Naughty little trick that I found here. Where our blog when accessed will automatically redirect to the latest articles, so that visitors […]
Thank you for this awesome tip (code)!
You rock Ashton Sanders!
Hi Ashton,
Thanks for this great tip!
How can I make the URL in the address bar to display the website URL and not the permalink of the first post even after the redirect?
Example: damnlol.com
Thanks!
Hi Aaish,
Well, not really. When you “redirect” to a new page, you end up on that new page. To make your home page be just a single post, you’d probably just have to program your own WP_Query on your page-home.php (and then make your home page named “Home”.
Enjoy,
Ashton
[…] to Ashton Sanders for the […]
I love this site