Forums

How to display 5 random excerpts on your front page? (13 posts)

  1. Anonymous
    Unregistered
    Posted 3 years ago #

    I would like to display 5 random post exceprts, along with their categories, in the format

    [excerpt 1] [category1, category2, category3]
    [excerpt 2] [category1, category2, category3]
    ..
    ..

    How can I do this? I was going through the codex and think I can use this as a starting point, but how do I change the syntax to choose posts randomly?

    query_posts(array('category__and'=>array(1,3),'showposts'=>2,'orderby'=>title,'order'=>DESC));
  2. VelvetBlues.com
    Member
    Posted 3 years ago #

    It would be something like this:

    <ul>
    <?php
    $rand_posts = get_posts('post_type=post&numberposts=5&orderby=RAND()');
     foreach( $rand_posts as $post ) :
    setup_postdata($post);
    ?>
    <li><?php the_excerpt(); the_category(', ') ?></li>
     <?php endforeach; ?>
    </ul>

    You can easily modify this to add permalinks, dates, etc...

  3. Anonymous
    Unregistered
    Posted 3 years ago #

    Thank you!

  4. Anonymous
    Unregistered
    Posted 3 years ago #

    Ok, tried this out and it does return 5 posts, but they don't seem random. They're the last 5 posts in descending order. Is there some other way to select randomly?

  5. VelvetBlues.com
    Member
    Posted 3 years ago #

    Do you have more than 5 posts? lol.
    The code should work... I've tried it out.

    WordPress even has a doc on it:
    See http://codex.wordpress.org/Template_Tags/get_posts#Random_posts

    This example omits the setup_postdata() command. (This command enables you to access all post data as opposed to just a few items such as title, permalink, etc..)

  6. Anonymous
    Unregistered
    Posted 3 years ago #

    Yes, got around 60 posts. Anyway I tried cutting and pasting the code above directly and it still gives the last 5 posts in order of recentness.

  7. VelvetBlues.com
    Member
    Posted 3 years ago #

    Very strange... Well, I'm at a loss.

  8. VelvetBlues.com
    Member
    Posted 3 years ago #

    What version WordPress are you running?

  9. alanft
    Member
    Posted 3 years ago #

    plugging "orderby=RAND()" into the query_posts string doesn't work for me in 2.5 :-(

  10. Anonymous
    Unregistered
    Posted 3 years ago #

    What was the problem? Was it just that the posts returned weren't random, or was it some syntax error?

  11. alanft
    Member
    Posted 3 years ago #

    if that was for me, the results weren't random - just got default (by post date) order

    however just looked in the query.php code and i think it's because it should be just "orderby=rand". hang on...

  12. alanft
    Member
    Posted 3 years ago #

    YES that works :-D

  13. Otto
    Tech Ninja
    Posted 3 years ago #

    Codex had that right, actually. You were looking at the wrong page. :)

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

    Although now the get_posts page is wrong, since get_posts was modified to use the same functionality as query_posts. I'll fix it.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.