WordPress.org

Ready to get started?Download WordPress

Forums

[closed] Display random posts on home page using the_post($arg) (11 posts)

  1. ld7
    Member
    Posted 3 years ago #

    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.

  2. Xephan
    Member
    Posted 3 years ago #

    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

  3. Gisha James
    Member
    Posted 3 years ago #

    I second Xephan.

    <?php query_posts('orderby=rand'); ?>
  4. krisjay
    Member
    Posted 3 years ago #

    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!

  5. brenocastroalves
    Member
    Posted 3 years ago #

    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?

  6. brenocastroalves
    Member
    Posted 3 years ago #

    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?

  7. alchymyth
    Forum Moderator
    Posted 3 years ago #

    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.)

  8. Tara
    Member
    Posted 3 years ago #

    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>
  9. brenocastroalves
    Member
    Posted 3 years ago #

    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

  10. justinadam
    Member
    Posted 3 years ago #

    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

  11. esmi
    Forum Moderator
    Posted 3 years ago #

    Please post a new topic.

Topic Closed

This topic has been closed to new replies.

About this Topic