$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How can I redirect my website's homepage to a random blog post?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How can I redirect my website's homepage to a random blog post?

matteradmin9PV0评论

I was trying to redirect my homepage to random blog post in WordPress.

My aim is to redirect my viewers to random blog posts whenever they enter into my website. I couldn't find a proper solution, can anyone help me?

I was trying to redirect my homepage to random blog post in WordPress.

My aim is to redirect my viewers to random blog posts whenever they enter into my website. I couldn't find a proper solution, can anyone help me?

Share Improve this question edited Feb 13, 2019 at 15:35 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 13, 2019 at 15:03 Vignesh RSVignesh RS 111 bronze badge
Add a comment  | 

3 Answers 3

Reset to default 1

If you don't want to use a plugin. You could put the re-direct directly into your .htaccess (on an apache server):

Place this code at the bottom of the .htaccess file:

redirect 301 / http://www.example/your-post-slug

make sure to change the url to the full url of your blog post.

If you don't have access to your .htaccess you could also do this in your cPanel (or other hosting control panel if you have one)

You can use template_redirect hook for that:

function my_page_template_redirect() {
    if ( is_home() ) { // change to is_frontpage(), if you use static page as front page
        $posts = get_posts( array(
           'post_type' => 'post',
           'orderby' => 'rand',
           'numberposts' => 1
       ) );
       if ( ! empty( $posts ) ) {
            wp_redirect( get_permalink( $posts[0]->ID) );
            die;
       }
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );

The quickest way to achieve this is to install the Redirect URL to Post plugin.

Via the query parameter, you can configure the homepage to load your latest post or a random one.

For example, your new homepage URL would be http://www.example/?redirect_to=random

You would likely need to change your home and site URL via WordPress settings. You can read more about this here.

Post a comment

comment list (0)

  1. No comments so far