Get random post in Wordpress

后端 未结 5 1124
你的背包
你的背包 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条回答
  • 2021-02-19 12:36

    I found this post which gave me desired results...

    Here's a solution copy/pasted from the wpbeginner blog post. No copyright infringement intended.

    Just add the following code to the functions.php file:

    add_action('init','random_add_rewrite');
    function random_add_rewrite() {
       global $wp;
       $wp->add_query_var('random');
       add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
    }
    
    add_action('template_redirect','random_template');
    function random_template() {
       if (get_query_var('random') == 1) {
               $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
               foreach($posts as $post) {
                       $link = get_permalink($post);
               }
               wp_redirect($link,307);
               exit;
       }
    }
    

    Use mydomain.com/random/ as your href for your button that leads to the random post.

    Thanks everyone who contributed for your help...

    Cheers!

    0 讨论(0)
  • 2021-02-19 12:38

    Another Simple solution to display Random Post

    1.First a create a custom page template. Name it as random post or a name of your choice!

    2.Open the page and remove the default wp loop and Paste the code below

    3.To change the no of post change the number ‘1’ to your choice!

     <?php
    query_posts(array('orderby' => 'rand', 'showposts' => 1));
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
     
    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
     
    <?php the_content(); ?>
     
    <?php endwhile;
    
    endif; ?>
    

    source: http://www.yengkokpam.com/displays-random-posts-in-a-page/

    0 讨论(0)
  • 2021-02-19 12:42

    Check This

    <ul>
    <?php
    $args = array( 'numberposts' => 5, 'orderby' => 'rand' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
       <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    
    0 讨论(0)
  • 2021-02-19 12:47

    I find it is more useful to have a URL that will redirect to a random post that you can use as link in sidebar or in menus. If it is a single WP site and even on wp.com it's really easy, for a blog at

    http://mygroovywpsite.me/
    

    All you need to do is append it with ?random

    http://mygroovywpsite.me/?random
    

    I found this did not work (nor the wp_beginner code above) on subsites in my multisite installation, either approach just loaded the home page. Maybe I had some funky cache issues. The way I do this on many sites is a few more steps w/o plugins.

    First make a Page in your site called "Random" / with the slug "random" -- it does not need any content in it

    Then create a page-random.php template

    <?php
    /*
    Random Post Picker
    Use on page to send viewer to random post optionally mod query
    */
    
    // set arguments for WP_Query on published posts to get 1 at random
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 1,
        'orderby' => 'rand'
    );
    
    // It's time! Go someplace random
    $my_random_post = new WP_Query ( $args );
    
    while ( $my_random_post->have_posts () ) {
      $my_random_post->the_post ();
    
      // redirect to the random post
      wp_redirect ( get_permalink () );
      exit;
    }
    ?>
    

    Then you get the re-direct for any link on your blog ...../random w/o any wrestling with .htaccess

    I've done it this way because I've had to modify the query, sometimes for custom post type, sometimes to restrict to category, etc.

    I only had one site that was a problem because the hosting suppressed the use of mySQL queries with ORDER BY RAND()

    0 讨论(0)
  • 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 '<li>';
        the_title();
        echo '</li>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    

    then in a page, just use:

    <a href="the link to the page">see a random post</a>
    
    0 讨论(0)
提交回复
热议问题