WordPress.org

Ready to get started?Download WordPress

Forums

Random post link? HELP (11 posts)

  1. phattrance
    Member
    Posted 2 years ago #

    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>

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    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>
  3. phattrance
    Member
    Posted 2 years ago #

    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?

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    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;
    }
  5. phattrance
    Member
    Posted 2 years ago #

    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.

    http://i.imgur.com/d3HjU.jpg

  6. keesiemeijer
    moderator
    Posted 2 years ago #

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

  7. phattrance
    Member
    Posted 2 years ago #

  8. phattrance
    Member
    Posted 2 years ago #

    Hi!

    did you have any luck fixing the bug?

  9. wingvictory
    Member
    Posted 2 years ago #

    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(); ?>

  10. angelforever123
    Member
    Posted 2 years ago #

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

    Thanks

  11. angelforever123
    Member
    Posted 2 years ago #

    alright i got it :) i knw that was quick :P may be i asked before i tried :D

Topic Closed

This topic has been closed to new replies.

About this Topic