Support » Plugins » Hacks » Display random posts on home page using the_post($arg)

  • Can i use any arguments to show random posts on home page with
    <?php while (have_posts()) : the_post(); ?>

    like the_post(orderby=rand); ???

    my wp blog lists some products on home page and i want to show random products each time a client visits.

    I’ve found lots of random_post plug-ins. but that was not applicable for me because i want the random product image also and that’s not on any sidebar etc. but as main posts on home page.

    please anyone help me
    thanks in advance.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You can always alter the posts that shows up in the loop by running query_posts with your required parameters before that. Read the documentation 🙂

    http://codex.wordpress.org/Function_Reference/query_posts

    I second Xephan.

    <?php query_posts('orderby=rand'); ?>

    I’m not too sure if this works with WP3 (haven’t tested it), but this will let you display a random set number of posts:

    query_posts(array('orderby' => 'rand', 'showposts' => 1));

    You can use that to display random posts like this:

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

    Where it states ‘showposts’, change the number to reflect the amount of posts you wish to show.

    Nice and simple!

    Not so simple, guys. I tryed both

    ‘<?php query_posts(‘orderby=rand’); ?>’

    ‘<?php query_posts($query_string . ‘&orderby=rand’); ?>’

    Put then in beginning of the loop and nothing happens.. I´m sure I´m missing something, maybe on home.php? Take a look:

    ‘<div class=”home-thumbs”>
    <?php $home_query = new WP_Query(“cat=&showposts=8”); $i = 0; ?>
    <ul class=”thumbs”>
    <?php while ($home_query->have_posts()) : $home_query->the_post();
    $do_not_duplicate = $post->ID; $i++; ?>’

    what do you think?

    Not so simple, guys. I tryed both

    ‘<?php query_posts(‘orderby=rand’); ?>’

    ‘<?php query_posts($query_string . ‘&orderby=rand’); ?>’

    Put then in beginning of the loop and nothing happens.. I´m sure I´m missing something, maybe on home.php? Take a look:

    ‘<div class=”home-thumbs”>
    <?php $home_query = new WP_Query(“cat=&showposts=8”); $i = 0; ?>
    <ul class=”thumbs”>
    <?php while ($home_query->have_posts()) : $home_query->the_post();
    $do_not_duplicate = $post->ID; $i++; ?>’

    what do you think?

    Michael

    (@alchymyth)

    Forum Moderator

    have you considered adding the ‘random’ to your $home_query code:

    <div class="home-thumbs">
    <?php $home_query = new WP_Query("cat=&showposts=8&orderby=rand"); $i = 0; ?>
    <ul class="thumbs">
    <?php while ($home_query->have_posts()) : $home_query->the_post();
    $do_not_duplicate = $post->ID; $i++; ?>

    (whatever you add in any code before, will be ignored and overwritten by your custom query.)

    Moderator t-p

    (@t-p)

    This is what iuse in the sidebar of my blog, it works for me:

    <li id="randompost" class="widget-container">
                <h3 class="widget-title"><?php _e( 'Random Post', 'twentyten' ); ?></h3>
                 <ul>
                 <?php
                   $rand_posts = get_posts('numberposts=1&orderby=rand');
                  foreach( $rand_posts as $post ) :
                  ?>
                 <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php endforeach; ?>
                </ul></li>

    alchymyth! so simple! you nailed it.

    I´m a writer/photografer beginning to learn css and php, this is my first work -> trocasvale.co.cc , a portfolio in progress.. got to dominate the tool..

    thank you all

    Good Day,

    My apologies for chiming in at the end of the post long after it finished, although I am implementing this code on my front page – and i am getting close with it. My issue is that I have a custom post type called ‘book’. Posts exist as well, although I don’t use those – the code is choosing from my ‘posts’ instead of my ‘book’.

    This is the piece of code that I am inserting into my home.php file:

    query_posts(array('orderby' => 'rand', 'showposts' => 10));

    This is the rest of my home.php code:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    If anyone could help, I would very much appreciate it.

    Regards,

    Justin

    esmi

    (@esmi)

    Forum Moderator

    Please post a new topic.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Display random posts on home page using the_post($arg)’ is closed to new replies.