WordPress.org

Ready to get started?Download WordPress

Forums

Displaying a random post on my front page (4 posts)

  1. Movieheart
    Member
    Posted 6 years ago #

    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.

  2. scormeny
    Member
    Posted 6 years ago #

    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!

  3. scormeny
    Member
    Posted 6 years ago #

    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.

  4. scormeny
    Member
    Posted 6 years ago #

    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...'); ?>
    
    <? }
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic