• Resolved slylake

    (@slylake)


    Hi all,

    I noticed at http://codex.wordpress.org/Template_Tags

    That the Category ID field is now deprecated.

    If I want to display a box in my sidebar with the last 5 or so posts to a specific category, how would I now do this? (I cannot see a replacement in the template tags page, instead just lists to help define things like breadcrumb trails).

    i.e.

    Two boxes in sidebar:

    Events:
    – post 5
    – post 4
    – post 3
    – post 2
    – post 1

    News:
    – post 5
    – post 4
    – post 3
    – post 2
    – post 1

    Any advice greatly appreciated! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • Put this twice in your sidebar.php. You could also use Otto’s PHP Exec Widget, if you use widgets and create two instances using this code. Of course you will change the cat=x to match your category IDs. I’ll leave you to handle the ‘headings’ if you need them.

    <?php
    $posts=get_posts('cat=1&showposts=5');
    if ($posts) {
    foreach($posts as $post) {
    setup_postdata($post);
    ?>
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <?php }
    }
    ?>

    There’s also this plugin:
    http://kwebble.com/blog/2007_08_15/archives_for_a_category

    Thread Starter slylake

    (@slylake)

    Awesome!! Thanks so much MichaelH!

    I’ve been trying to accomplish the same thing. Tried Otto’s, tried what feels like everything, and this was my last resort. It worked perfectly for the index page, but as soon as I selected a page from the list (which goes to the single.php template) it breaks. Basically what happens is that the URLs change accordingly, but it always shows the most recent post when I get to single.php. It’s odd to see the address bar change, but the pages remain the same.

    I put the php snippet in a separate sidebar template and called that template on index.php and single.php in case that’s not clear.

    Any ideas? I’m stuck.

    Also, if I use the “Recent Posts” widget in the sidebar, it works perfectly. The only issue there is that I can’t show the X amount most recent from different categories.

    EDIT: Just as I was about to give up, I found the Enhanced Recent Posts plugin. Worked like a charm.

    http://wordpress.org/extend/plugins/enhanced-recent-posts/

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Displaying Posts by Category’ is closed to new replies.