Dukeo » WordPress » Redirect to First Post: How to Redirect your Homepage

Redirect to First Post: How to Redirect your Homepage

Steven 12 responses WordPress

You might want to develop a blog, based on WordPress CMS, with no homepage. In that case, the solution is to have your visitors automatically redirected from your homepage to your latest blog post. In order to do this, there are 2 steps that needs to be done.

First of all, paste the following code at the beginning (before everything else) of the index.php file in your theme folder:

<?php
if(is_home()) {
  if (have_posts()) {
    while (have_posts()) {
      the_post();
      wp_redirect(get_permalink());
    }
  }
} else {
?>

And lastly, paste the following code at the end (after everything else) of the index.php file in your theme folder:

<?php } ?>

If you enjoyed this article,
Get email updates

Join for Free »

12 Comments

  1. Paul Salmon

    While I haven’t seen too many blogs that utilize this feature, I’m sure it can come in handy. Perhaps you may even want to redirect to a specific page as well, maybe one that shows a limited time offer.

    1. Absolutely! Redirecting your visitors to a special offer page can be quite interesting…

  2. Sarah

    Interesting… I’ll have to try this out! Thanks!

  3. Shad

    Awesome, bro! You’re the best, I dunno what I would do without these posts. Definitely using this, thanks!

  4. Dana

    Oh my, this is just like a foreign language to me ha ha! I love that you post these, but it always makes me wonder what each little character is for, and what they mean. So many question marks, what are they there for? Probably to confuse me even more ha ha! Thanks again for sharing your knowledge with us!

    1. Anon

      It’s a secret language. Top secret. The question marks are there to deter you.

    2. Dana

      I knew it! Haha, I think @AffMaster may be right, it may be time to force myself to learn. It’s tough to teach an old dog new tricks, but I think it will be good to challenge myself. After all, it’s the New Year!

  5. Affmaster

    @Shad and @Dana: Take a class, it’s really not as difficult as you’re making it out to be! And while we should definitely be grateful to be able to use this site to our advantage, you can learn this for yourself too, don’t be shy! @Paul: I think you’re really onto something there…

    1. Shad

      I definitely think you’re right on that one, bro. It’s just a time thing, y’know? Like, where in the world am I gonna find the time to take a class on this? And it’s pretty intimidating…

    2. Affmaster

      Don’t be intimidated. All that means is that you’re the only thing in your way. Just dominate it. I promise you, it will do wonders for your career and your self-esteem, it makes you feel so great when you conquer something. Make the time, take night classes. Even if you only just get a basic understanding, because right now you’re limiting your own true potential.

  6. Liz

    I may steal this one too! I’m sure there could be a lot of different ways to use it too.

  7. Samie

    It’s always so nice of you to post these. To repeat what Dana said, thank you for sharing your wisdom with us!