Support » Developing with WordPress » Random post or page on homepage

  • sieborg007

    (@sieborg007)


    Hi πŸ™‚

    I would like to display a random page or post on my homepage. The post should be shown entirely, in full page. Each time you refresh the page (in this case http://www.slammingdoors.space) it should show another post from a canvas of about 100 posts).
    Each post has different content, it can be an image, a video, only sound etc.).

    I searched a way to do this with plugins, but every tentative was a miss πŸ™

    So I created a new page called “random” where the idea is to put some code to display a random post (or page if it’s easier).

    Would there be a simple way to do that? If it’s not simple, it’s ok too πŸ˜€

    I already created 2 posts as an example so I could try what you suggest immediately.

    Note that the url of the homepage shouldn’t change! Only the content. So it should stay http://www.slammingdoors.space.

    Also I use elementor so I could easily add code with a code widget on the page.

    Thanks for your precious help !

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • tugbucket

    (@tugbucket)

        <?php
        $args = array(
            'post_type'      => 'post',
            'orderby'        => 'rand',
            'posts_per_page' => '1',
        );
        $my_query = new WP_Query( $args );
        if ( $my_query->have_posts() ) {
            while ( $my_query->have_posts() ) {
                $my_query->the_post();
    
                /* do your post output here */
    
            } // end while
        } // end if
    
        ?>

    something like that should work.

Viewing 1 replies (of 1 total)
  • The topic ‘Random post or page on homepage’ is closed to new replies.