WordPress.org

Support

Support » How-To and Troubleshooting » List of 5 Most Recent Posts Within a Category

List of 5 Most Recent Posts Within a Category

  • In the sidebar on my post pages, I want a box that shows the 5 most recent posts within the same category as that post only.

    For example, I write movie reviews. If someone goes to my most recent movie review, I want them to see a list of the previous 5 movie reviews, but NOT any other posts from any other category.

    Is this possible? I’ve been searching and trying things like crazy, but I can’t figure it out.

    http://www.badlit.com

Viewing 15 replies - 1 through 15 (of 39 total)
  • Try this.

    <?php $posts = get_posts( "category=" . $id . "&numberposts=5″ ); ?>
    <?php if( $posts ) : ?>
    <div class="recent-posts">
    <h2>More on this category</h2>
    <ul>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    <li><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </div>
    <?php endif; ?>

    Thanks. However, that gives me a Parse error. I gotta find out what Line 66 is.

    Parse error: parse error, unexpected T_STRING in /home/.necho/meverleth/badlit.com/wp-content/themes/connections/single.php on line 66

    Alpha, I tried a couple places and didn’t get that to work yet. Should I be putting that inside the loop or outside the loop, in my (default) Sidebar Template or in my Category Template?

    I’d like this too.

    The error turned out to be a bad quote. Now my pages are just ignoring this code.

    Dgold, that’s code for outside the loop.

    is everyone replacing $id with the actual id of the category you want to display?

    miklb:

    Replacing the $id with the category number does work. However, that’s not the point of what I want to do. I don’t want to plug in an absolute category number. I need to call up the category number of whatever category the post is.

    For example, on a movie review post, I want to display the previous 5 movie reviews. But, on a comic book review page, I want to display the previous 5 comic book reviews. And I can’t do that if I have to plug in a number. I need something that will pull up the category of whatever the post belongs to.

    So, instead of putting in a category number in this code, is there something I can put in that will call up the category of the post and plug it in?

    I don’t get why $id is in the example at all, but it finally worked for me when I used this example
    <?php $posts = get_posts('category=2&numberposts=5'); ?>
    it was the quote marks and category= that I needed to fix

    This IS useful for me although I am also interested in the list of headlines from “whatever category I’m in” not a set category for my Category Template

    My bad. Replace $id with $cat
    That way, you’ll get recent posts of whatever category the post you’re viewing.

    Alphoide, I’ve been playing around with this, and I can’t get your example to work.
    <?php
    $cat = get_the_category(); $cat = $cat[0]; echo $cat->cat_ID;
    ?>

    Will output the current category ID. How to work that into the get_posts is the problem I’m having.

    My apology for keep giving inaccurate answer. The following code has been tested. The code is to be put on the file single.php, after and outside the main Loop.

    <?php
    $cat = get_the_category();
    $cat = $cat[0];
    $posts = get_posts("category=" . $cat->cat_ID . "&numberposts=5");
    if( $posts ) :
    ?>
    <div class="recent-posts">
    <h2>More on this category</h2>
    <ul>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    <li><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </div>
    <?php endif; ?>

    And if someone wants to include it in the sidebar, they can simply do an if…else statement or load a seperate sidebar for the single.php page.

    alphaoide:

    Thank you, thank you, thank you!! I really appreciate you sticking with this thread I started. I pasted in that last piece of code you posted and it works exactly the way I want it! I need to play around with the layout, but this is perfect.

    Thanks again!

    Also, for any future users: I pasted this code in the Post template in the sidebar div, but above the code that calls up my official Sidebar template (that way it doesn’t show up on the homepage).

    Okay, I got it sort-of working. I put the latest code from alphaoide on my Post Template (single.php). The problem was, my “Comments” area displayed comments from the 5th headline, not from the post I was looking at.

    For now, I’ve moved the Recent Headlines list below the Comments field, and it’s working. Any ideas? Thank you all.

Viewing 15 replies - 1 through 15 (of 39 total)
  • The topic ‘List of 5 Most Recent Posts Within a Category’ is closed to new replies.