• 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 9 replies - 31 through 39 (of 39 total)
  • hm.,,

    does anyone here know if it’s possible to get 5 recent posts from a desirable catagory and let it displays in a website
    ( ie help.php). don’t want show the links but rather show a whole contents.

    possible or not?

    Erb, I’ve got the code you have posted working at the bottom of my entries (versus in the sidebar). Do you know how I would modify the code you posted to sort the results alphabetically by title instead of by date? Thanks!

    ibeats

    (@ibeats)

    has there been any resolution to this. i would like the latest post from any of my 6 categories to be displayed, with the next 5 from any category to be shown underneath by title.

    this is close, but lists only other posts as headlines from the same category 🙁

    This code is great and something I’ve seen in action on a lot of sites. Thanks so much. For those interested I altered it just a tad and put it in my sidebar.php. What I was looking for was to display all the post titles in whatever category I choose. Now I can add or subtract a category at will (manually of course). He’s the code I used (I took out the $cat globals so I could maually select the category):

    <div id="sidebar">

    <?php
    $posts = get_posts("category=1" . "&numberposts=20");
    if( $posts ) :
    ?>

    <div class="recent-posts">

    <h4>Exhibitions</h4>

    <h5>2006-2007</h5>

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

    <?php
    $posts = get_posts("category=2" . "&numberposts=5");
    if( $posts ) :
    ?>

    <div class="recent-posts">

    <h4>News &amp; Events</h4>

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

    </div>

    sfam – I think you could make the order alphabetical quite easily with the addition of &order=ASC&orderby=post_title in the get_posts() request:

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

    See information here:
    http://codex.wordpress.org/Template_Tags/get_posts#Generate_another_list

    This seemed to work for me, but I was testing it on a set up that appears quite different from yours. HTH!

    And FWIW, here’s the current version I’m using, which excludes a specific category (I use have one category hold all posts, sort of a default) in the listing and also puts each list in a new table cell (although this could be modified easily to divs, ids, and classes should one prefer) at most three columns wide.

    <?php $original_post = $post;
    /* insert the whole code to get recent posts of same category */ ?>
    <div class="all-recent-posts">
    <table border="0" cellpadding="0" cellspacing="0" class="recent-posts"><tr><?php
    $r=1;
    foreach((get_the_category()) as $category) {
    if($category->cat_ID==1){
    //do nothing: ignore category #1
    }else{
    $posts = get_posts("category=" . $category->cat_ID . "&numberposts=5&orderby=post_title");
    if( $posts ) : ?>
    <td valign="top" width="33%">
    <h4><span class="small10" style="color:#399;">Most Recent in</span><br><?=$category->cat_name?></h4>
    <ul>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    <li><span class="date"><?php the_time("m/d/y") ?></span> - <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </td>
    <?php
    if(fmod($r, 3)==0){
    echo "</tr><tr> <!--" . fmod($r,3) . "-->";
    }else{
    echo "<!--nothing to see here--> <!--" . fmod($r, 3) . "-->";
    }
    $r=$r+1;
    endif;
    }
    }
    ?>
    </tr></table></div>

    Hopefully those more knowledgeable than I can make this even better!

    Thanx, this was what I needed, there is only one slight problem. When I check the categorie news, it only shows the posts listed in this category. But when I check another categorie it shows the posts listed in that categorie, but in NEWS ass well.
    How can I adjust the code in that way that it will only show the post for that particular categorie?

    anyone?

    The codes in this thread work great except when a post is in multiple categories. Any gurus out there who can help?

    klifix and bokinuk, I believe this is what you are looking for:

    <?php
    $posts = get_posts("category=2" . $cat->cat_ID . "&numberposts=5");
    if( $posts ) :
    ?>
    <h2>Category Title </h2>
    <ul class="cat class">
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    <li class="list class" >" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?>
    <?php endforeach; ?>

    <?php endif; ?>

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