0

I am trying to rotate 3 pages as my home page at refresh.

Currently I am using this in my functions.php:

<?php
function my_homepage_redirect() {
    if ( is_home() || is_front_page() ){
        $page = get_posts( [
            'post_type' => 'page',
            'posts_per_page' => 1,
            'orderby' => 'rand',
            'fields' => 'ids'
        ] );

        if ( empty( $page ) ){
            return;
        }

        wp_redirect( get_permalink( $page[0] ) );
        exit;
    }
}
add_action( 'template_redirect', 'my_homepage_redirect' );
?>

But I don't want a redirect. I'd like for the URL to stay domain.com.

Probably I need something like this (so that it just sets a page between the 3 as home page rather than redirecting), but how do I build this in functions.php?

1 Answer 1

0

Not tested well but you can try this.

add_action( 'template_redirect', 'my_homepage_redirect' );
function my_homepage_redirect() {
    if ( is_home() || is_front_page() ){
        $page = get_posts( [
            'post_type' => 'page',
            'posts_per_page' => 1,
            'orderby' => 'rand',
            'fields' => 'ids'
        ] );
        
        if ( empty( $page ) ){
            return;
        }

        // update the front page id option according to the page id
        update_option( 'page_on_front',  $page[0]);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.