Get random post in Wordpress

后端 未结 5 1242
你的背包
你的背包 2021-02-19 12:00

How do I get a random post in Wordpress?

I would like to display a button on a page that, when pressed, goes to a random post from the blog. I don\'t want a random post

5条回答
  •  Happy的楠姐
    2021-02-19 12:51

    create a page template, and use the following code to get a random post:

    //Create WordPress Query with 'orderby' set to 'rand' (Random)
    $the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
    // output the random post
    while ( $the_query->have_posts() ) : $the_query->the_post();
        echo '
  • '; the_title(); echo '
  • '; endwhile; // Reset Post Data wp_reset_postdata();

    then in a page, just use:

    see a random post
    

提交回复
热议问题