Website Designers and Webmasters

Dedicated to all the tasks Webmasters, Website Developers and Website Designers find themselves facing.

WordPress: Redirect Blog to First Post

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 template (home.php) 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

(P.S. If you haven’t heard, Adobe just released the pre-order for CS5… If I were to get one of the packages today, I’d probably get: Adobe Web Premium CS5. It looks ridiculously epic.)

Tags: , , , , ,

14 Comments »

  1. 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.

    Comment by mark — October 18, 2010 @ 10:22 am

  2. [...] to Ashton Sanders for the [...]

    Pingback by How to redirect your homepage to the first post | WordPress Toolbag — February 4, 2011 @ 5:01 pm

  3. [...] { 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 [...]

    Pingback by How to redirect your homepage to the first post - Broadys Web Design — February 4, 2011 @ 6:11 pm

  4. Useful snippet, thanks!

    Comment by Theo — February 5, 2011 @ 1:19 am

  5. [...] 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 [...]

    Pingback by Redirect Your Visitors To Latest Post Instead Of Wordpress Homepage — February 6, 2011 @ 12:12 pm

  6. [...] 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 [...]

    Pingback by How To Redirect Blog to First Post # WordPress Tricks & Tips — February 8, 2011 @ 1:49 am

  7. [...] { 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 [...]

    Pingback by How to redirect your homepage to the first post - WPInsite — February 28, 2011 @ 5:09 pm

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

    Pingback by WP-Rady pro pokro?ilé - Jak p?esm?rovat úvodní stránku na první p?ísp?vek — May 5, 2011 @ 3:30 pm

  9. [...] 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 [...]

    Pingback by Automatic Redirect to New Article on WordPress — September 28, 2011 @ 12:36 am

  10. Thank you for this awesome tip (code)!

    You rock Ashton Sanders!

    Comment by Aasish — May 31, 2012 @ 5:12 am

  11. 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!

    Comment by Aasish — June 1, 2012 @ 2:23 am

  12. 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

    Comment by Ashton Sanders — June 1, 2012 @ 12:44 pm

  13. [...] to Ashton Sanders for the [...]

    Pingback by How to redirect your homepage to the first post | WP Tutorials | Summer Study — August 6, 2012 @ 1:57 am

  14. I love this site

    Comment by Jeremiah — February 11, 2013 @ 2:54 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment