WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. bravecreatures
    Member
    Posted 8 years ago #

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

  2. cam_oai
    Member
    Posted 8 years ago #

    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?

  3. sfam
    Member
    Posted 8 years ago #

    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!

  4. ibeats
    Member
    Posted 8 years ago #

    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 :(

  5. Ryan Fitzer
    Member
    Posted 7 years ago #

    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>

  6. erb
    Member
    Posted 7 years ago #

    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!

  7. klifix
    Member
    Posted 7 years ago #

    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?

  8. klifix
    Member
    Posted 7 years ago #

    anyone?

  9. bokinuk
    Member
    Posted 7 years ago #

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

  10. supor1t
    Member
    Posted 7 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.