WordPress.org

Ready to get started?Download WordPress

Forums

List of 5 Most Recent Posts Within a Category (40 posts)

  1. badMike
    Member
    Posted 8 years ago #

    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

  2. Joshua Sigar
    Member
    Posted 8 years ago #

    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; ?>

  3. badMike
    Member
    Posted 8 years ago #

    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

  4. Dgold
    Member
    Posted 8 years ago #

    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.

  5. badMike
    Member
    Posted 8 years ago #

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

  6. Michael Bishop

    Posted 8 years ago #

    Dgold, that's code for outside the loop.

  7. Michael Bishop

    Posted 8 years ago #

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

  8. badMike
    Member
    Posted 8 years ago #

    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?

  9. Dgold
    Member
    Posted 8 years ago #

    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

  10. Joshua Sigar
    Member
    Posted 8 years ago #

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

  11. Michael Bishop

    Posted 8 years ago #

    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.

  12. Joshua Sigar
    Member
    Posted 8 years ago #

    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; ?>

  13. Michael Bishop

    Posted 8 years ago #

    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.

  14. badMike
    Member
    Posted 8 years ago #

    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!

  15. badMike
    Member
    Posted 8 years ago #

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

  16. Dgold
    Member
    Posted 8 years ago #

    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.

  17. Joshua Sigar
    Member
    Posted 8 years ago #

    $original_post = $post;
    /* insert the whole code to get recent posts of same category */
    $post = $original_post;
    setup_postdata($post);
    /* insert the code to get comments for currently-viewed post */

  18. erb
    Member
    Posted 8 years ago #

    This is a great little snippit, so thanks alphaoide. I would like to modify it so that it can accomodate posts that belong to more than one category; if anyone has any tips in this regard, they would be much appreciated.

  19. Tijmen
    Member
    Posted 8 years ago #

    Anyone has an idea to modify this code to make it show posts in every category the post is in? I use multiple categories for almost every post on my site.

  20. erb
    Member
    Posted 8 years ago #

    I think I have a solution for the multiple-category issue. This works for me, anyway. Anyone who is better with coding, feel free to improve upon this:


    <?php
    foreach((get_the_category()) as $category) {
    $posts = get_posts("category=" . $category->cat_ID . "&numberposts=5");
    if( $posts ) :
    ?>
    <div class="recent-posts">
    <h2>Most Recent in <?=$category->cat_name?></h2>
    <ul>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    <li><a>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </div>
    <?php endif; ?>
    <?php } ?>

  21. polyfade
    Member
    Posted 8 years ago #


  22. Joshua Sigar
    Member
    Posted 8 years ago #

    I need to see the problematic site to troubleshoot this.

  23. sfam
    Member
    Posted 8 years ago #

    Wow, this is a great idea! I'm interested in using this same idea for similar reasons to badMike, but I'm using the Semiologic CMS pluggin and them. This doesn't have a single.php page. Does anyone know where I should try inserting the code into the Semiologic theme?

  24. bravecreatures
    Member
    Posted 8 years ago #

    ok so here is how I am trying to modify things

    I display 5 posts on a page (by conincidence) and I want this area of my sidebar to show the NEXT five posts in the same category (so to sart with it would show #6 thorugh #10).

    Does anyone have a suggestions?

    Thanks

  25. Joshua Sigar
    Member
    Posted 8 years ago #

    Pass the "offset" parameter.

    ...
    $posts = get_posts("category=" . $category->cat_ID . "&numberposts=5&offset=5");
    ...

  26. bravecreatures
    Member
    Posted 8 years ago #

    Thanks so much for your help. I am almost there, it is kind of working.

    I am using

    <?php
    foreach((get_the_category()) as $category) {
    $posts = get_posts("category=" . $category->cat_ID . "&numberposts=5&offset=5");
    if( $posts ) :
    ?>
    <div class="recent-posts">
    <h2>Most Recent in <?=$category->cat_name?></h2>
    <ul>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li><br />
    <?php endforeach; ?><br />
    </ul>
    </div>
    <?php endif; ?>
    <?php } ?>

    I have it in the sidebar, but I am not sure that is going to be right.

    I want it to show up when a reader looks at

    a/ the main page (so it shows the titles of post 6 though 10)
    b/ a category listing, aslo showing post 6-10 for that category

    but it is getting stuck in showing only the things for the last category selected on both the page and in the side bar.

    Any suggestions?

  27. sfam
    Member
    Posted 8 years ago #

    erb, your code is almost exactly what I want (except that I want all the posts from that category showing up, but I can modify the number).

    My question is a real newbie question - how do I load this into my side bar? Even more specifically, how do I make a different side bar for single posts, and where do I call it from my single.php file? Thanks in advance.

  28. sfam
    Member
    Posted 8 years ago #

    Just an follow-up, I think see two separate things I'm supposed to do. I have the single.php file that looks like this:
    <?php
    require_once(TEMPLATEPATH . '/header.php');

    the_posts();

    require_once(TEMPLATEPATH . '/footer.php');
    ?>

    Then I think I'm supposed to do something with these two commands - I've tried various ways of combining them, to no avail:

    <?php get_sidebar(); ?>
    <?php include (TEMPLATEPATH . '/mysidebar.php'); ?>

    I'm still only at best getting this code working below my post. I have the code loaded into the mysidebar.php file, and can get it showing up at the bottom of the post, but that's it. For an example, see:
    http://cyberpunkreview.com/movie/decade/1980-1989/cherry-2000/

  29. bravecreatures
    Member
    Posted 8 years ago #

    I think I have just about got this worked out, I just need one last pointer.

    The more categories list lives in my sidebar and is working fine. I have even got it only showing when a specific category is choosen. What is still going wrong for me is this offset.

    When I add the offset code, its offsetting the whole thing - the body of the page as well as the list in the sidebar. Which is obviously not what I need.

    Does anyone have any suggestions/

  30. slambert
    Member
    Posted 8 years ago #

    once this is sorted, please post it on the codex!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.