WordPress.org

Ready to get started?Download WordPress

Forums

Query Posts-previous five posts how? (8 posts)

  1. lokjah
    Member
    Posted 9 years ago #

    Hey there, I would like to use the admin panel setting to show a certain number of posts on my blog say 5, but then I would like to do a query posts after that initial loop and show a list of the previous 5 posts (post id and title permalink only). I want it to be category independent, meaning it doesnt matter what category the posts are in, just show the next 5 in chronological order.

    *and as an alternate, is there a way to show the next 5 as a random list also?

    the one I've got now shows 5 posts but it shows the same 5 posts as the initial loop (I have 5 set in wp admin to show)

    <ul id="previously">
    <?php query_posts('showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <li><?php the_ID(); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"></a></li>
    <?php endwhile; ?>
    </ul>

    thanks

  2. Firas
    Member
    Posted 9 years ago #

    Just a tip: if you only want to show id and permalink, use the much easier get_posts.

  3. lokjah
    Member
    Posted 9 years ago #

    ah cool, so would I do something like:

    $posts = get_posts('numberposts=5&offset=5);
    foreach($posts as $post) :
    ?>

    to show the previous 5 after the first loop of 5?

  4. lokjah
    Member
    Posted 9 years ago #

    sweet that worked dope! thanks firas....

    now how about getting that previous 5 as random titles?

  5. Firas
    Member
    Posted 9 years ago #

    Try doing shuffle($posts); right before the foreach loop?

  6. lokjah
    Member
    Posted 9 years ago #

    hmmm close firas

    that seems to shuffle the order of the outputted post list, what I really wanted was it to pull random posts from all my posts..

    this shuffle is cool tho..

    maybe there isnt enough parameters in the get_posts to do this and I might need to go back to a second loop?

  7. lokjah
    Member
    Posted 9 years ago #

    ah one more thing.. im getting the same post id for each of those titles, and its actually the same post id as the last one in the first loop..

    eg, the last post in my blog (#5) is post id 88, the code I'm using below shows each of these next 5 as post id 88 also, the titles work fine but not the id..

    <ul id="previous">
    <?php
    $posts = get_posts('numberposts=5&offset=5');
    //shuffle($posts); shuffles order of output posts
    foreach($posts as $post) :
    ?>
    <li><?php the_ID(); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endforeach; ?>
    </ul>

    in action_

    http://clintfisherart.com/news/

  8. lokjah
    Member
    Posted 9 years ago #

    argh.. helps if I read...

    setup_postdata($post);

    thats sussed... :)

    just need to know how to do the random one now so it grabs 5 from all my posts available, rather than the 5 in the offset...

    any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic