Are you looking for a way to redirect your home page to a random post automatically? While there’s probably a plugin for this, we have created a quick code snippet that you can use to add a custom feed footer in WordPress.
Instructions:
All you have to do is add this code to your theme’s index.php file:
<?php if (have_posts() && is_home()) { query_posts('orderby=rand'); while (have_posts()) { the_post(); wp_redirect(get_permalink()); } } ?>
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 62 best free WordPress blog themes and how to set up author tracking in WordPress with Google Analytics.
Thank you so much for your information Kevin, this post is really helpful.
[…] “Redirect Home Page to a Random Blog Post,” WordPress Code Snippets […]
I have been Googling all morning on how to make my blog display random posts on homepage and this truly done it! Thank you Kevin. You just saved me.
Sweet code. I used this on my multisite install in a different way. I created a page template with this in it, then created a page with that template as the page template. I then added a “Random Post” button to the theme that all my multisites use that links to that page. So the result is a button that users can continually click to get to a random post on a random blog on the network. fyi this requires that you use the sitewide tags plugin to repost any posts from sub blogs onto the main blog. It sounds complicated maybe but it works great!
Sweet sounds cool Paul, you could do something similar with this if you created a new template and page called random or something with the above. Hey you should submit a snippet or two we just recently add a contribute option – http://wpsnipp.com/index.php/contribute/
[…] code snippets and shortcodes almost everyday of the week!Here are some of our favorite ones: Redirect a home page to a random post, show taxonomies as a list on a post or page, and disabling widgets on certain pages! Share and […]