WordPress.org

Support

Support » How-To and Troubleshooting » Random post link? HELP

Random post link? HELP

  • Is it possible to add a random post link in wordpress?

    Go to this page: http://funny-pictures-blog.com/2011/06/07/funny-pictures-bad-day-2/ below the image u can see the next and previous links, i want to have a link in the middle were it says “Random post”. that link should randomly choose a post and display it for the reader. is that possible to add?

    i am using this code for the next and previous links:

    <div class=”nav-box next” style=”float:right;”><h2><?php previous_post_link(‘%link’, ‘Next Post >>>’, false); ?></h2>
    </div> <div class=”nav-box next” style=”float:left;”><h2><?php next_post_link(‘%link’, ‘<<< Previous Post’, false); ?></h2>
    </div>

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Try this:

    <div class="nav-box next" style="float:right;"><h2><?php previous_post_link('%link', 'Next Post >>>', false); ?></h2></div>
    
    <?php
    $random_post = get_posts('posts_per_page=1&orderby=rand');
    $link = '<a href="'.get_permalink($random_post[0]->ID).'" title="'.get_the_title($random_post[0]->ID).'">'.get_the_title($random_post[0]->ID).'</a>';
    ?>
    <div class="nav-box random" style="float:left;"><h2><?php echo $link; ?></h2></div> 
    
    </div> <div class="nav-box next" style="float:left;"><h2><?php next_post_link('%link', '<<< Previous Post', false); ?></h2></div>

    It looks like this now: http://i.imgur.com/tAOs6.jpg

    i want the random post to be in the middle and also i dont want the links name to be the post title, just want it to be called “Random Post”

    possible to fix this?

    Moderator keesiemeijer

    @keesiemeijer

    Try it with this:

    <div class="nav-box next"><h2><?php next_post_link('%link', '<<< Previous Post', false); ?></h2></div> 
    
    <?php
    $random_post = get_posts('posts_per_page=1&orderby=rand');
    $link = '<a href="'.get_permalink($random_post[0]->ID).'" title="Random Post:'.get_the_title($random_post[0]->ID).'">Random Post</a>';
    ?>
    <div class="nav-box random" ><h2><?php echo $link; ?></h2></div> 
    
    <div class="nav-box previous"><h2><?php previous_post_link('%link', 'Next Post >>>', false); ?></h2></div>

    and put this in your theme’s stylesheet (style.css?).

    div.nav-box {
    width: 32%;
    }
    
    div.nav-box.next, div.nav-box.random {
    float: left;
    }
    
    div.nav-box.previous {
    float: right;
    }

    Thank you, it works better now. i have one question tho! is it possible to move the “next post” more to the right? as you can see in this screenshot, theres still a lot of available space to place the next page link. i would like to have the previous post as much to the left as possible, the random post in the middle and the next post as much to the right as possible.

    View post on imgur.com

    Moderator keesiemeijer

    @keesiemeijer

    Then I need a link to the page in question. I can only guess right now how your html structure and stylesheet rules are.

    Hi!

    did you have any luck fixing the bug?

    hi I am trying to do this too. i am new so people bare with me.

    I’ve been trying to post the Code in single.php (both for the next/previous/random buttons) nothing is working. here is my coding without the button text…where do I paste the code to make the new links?

    <?php get_header(); ?>

    <!– /////////////////////////////////////////////////////////////////////// –>
    <!– // // –>
    <!– /////////////////////////////////////////////////////////////////////// –>
    <div id=”content_container”>

    <div id=”content_wrapper”>

    <?php include “templates/”.get_single_cat_type().”/”.get_cat_single_template().”.php”; ?>
    </div><!– END div#content_wrapper –>

    </div><!– END div#content_container –>
    <?php get_footer(); ?>

    Hi keesiemeijer, Is there a way to pull random posts from a particular categories ?

    Thanks

    alright i got it 🙂 i knw that was quick 😛 may be i asked before i tried 😀

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Random post link? HELP’ is closed to new replies.