WordPress.org

Support

Support » How-To and Troubleshooting » Displaying a random post on my front page

Displaying a random post on my front page

  • I’m looking for a code that I can use for displaying a random post on my front page. I have found som plugins but they only make a list of random posts. I want the full post or the excerpt and an image. Just like a normal post on the front page.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Bump — I have this same question, using WordPress 2.5.1 . My site sandbox is at http://www.managebest.net/tandp . Rather than the most recent post, I want the front page to have a random post.

    I tried editing .htaccess to ?random, but either that doesn’t work or I did it wrong, need a better solution from a far brighter programmer than I am.

    Thanks for advice!

    I surfed through the forums and after much testing found that the solution here worked for me:


    http://wordpress.org/support/topic/174095?replies=6

    I tweaked the display section, and now my page is working properly.

    Here’s the code, for reference; when I say I “tweaked the display section,” I mean that I

    <?php
    
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY RAND() LIMIT 1");
    
    foreach($rand_posts as $post){
    
    	setup_postdata($post);
    ?>
    
    Put whatever you want to display here, for example: <?php the_content('Read more...'); ?>
    
    <? }
    
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Displaying a random post on my front page’ is closed to new replies.