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

    https://www.badlit.com

Viewing 15 replies - 16 through 30 (of 39 total)
  • $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 */

    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.

    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.

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

    I need to see the problematic site to troubleshoot this.

    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?

    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

    Pass the “offset” parameter.

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

    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?

    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.

    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:
    https://cyberpunkreview.com/movie/decade/1980-1989/cherry-2000/

    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/

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

    I would if I could but I am having no joy on this one – sorry

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