Support » Fixing WordPress » recent posts from the same category

  • Resolved moshu


    I know I saw it somewehere… just can’t find it 🙁

    I need a code snippet for a sidebar (query?) that would display the X number of recent posts in single post view – but from the same category as the post is in.
    Basically, the code should be smart enough to take the category of the viewed single posts and query the recent posts ONLY from that cat.

    Anybody remembers anything like that? Thanks!

Viewing 15 replies - 1 through 15 (of 17 total)
  • Well… I’m 99% sure this is not what you want, but… 🙂

    To do so, I use conditional tags in my sidebar with an rss feed from the category…

    Of course, it’s not automatic, I have to update the side bar manually each time I add a category…

    <?php if ( in_category('1') ): ?>
    <?php endif; ?>
    <?php if ( in_category('2') ): ?>
    <?php endif; ?>

    I know, I know, this is not what you want… 🙂 I would prefer a code like the one you search, but as I didn’t find one, this is the way I found to do so… 🙂


    Mais no…

    I have “workarounds” – but I want the CODE.
    (Unfortunately, you have just removed this topic from the unanswered queue, so nobody will take a look at it)

    You’d use get_posts, right?

    $myposts = get_posts(‘numberposts=x&category=y’)

    Then loop through ’em for display.

    THAT is my problem. I don’t want that. I want the code to take the cat ID from the single post we are looking at…

    moshu said : “(Unfortunately, you have just removed this topic from the unanswered queue, so nobody will take a look at it)”

    Well… Sorry :-))) (!)

    There is also a plugin to do so :


    Ah, thanks for the plugin, Simon. It almost does what I need. Have to figure out how to use it in the sidebar because it is one of those “automatic” plugins when they think they know what I want 🙂

    Ended up using the plugin above – just disabled the automatic display and inserted this into my sidebar:

    <?php if (is_single()): ?>
    <?php echo ddop_show_posts(); ?>
    <?php endif; ?>

    The output will behave according the the options you define in the plugin’s admin page.

    Yeah… I, too, hate these kind of plugins with too much gizmos and this is why I don’t use it.

    But as you have better coding knowledge than me, it could be an inspiration to write a short and sweet function…

    At the middle of his code : “function ddop_show_posts”

    And later : “// get list of cats this post is in”

    I already gave a try to simplify it in a simple fuction to do what you wanna do, but It was more complicated for me than reading Heidegger…

    Let me know if you find one… I will get rid of my workaround… 😉


    Check the post above yours – it works for me!

    Just to follow up on my silly example. to get the cat_id:

    $cat = get_the_category(); $cat = $cat[0];

    Would get you the first category assigned.

    Well, well… as you know, the challenge for me is always to put together two pieces of code to make them work, since I am completely php illiterate.

    As I said, it works with the plugin (although had to modify the plugin to make it aware of the Pages-related changes in the DB; to exclude them). You can see it at my “blogtest”.

    You can do it with this bit of code:

    global $post;
    $categories = get_the_category();
    $category = $categories[0];
    $cat_ID = $category->cat_ID;
    $myposts = get_posts("numberposts=20&category=$cat_ID");
    <?php foreach($myposts as $post) :?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endforeach; ?>

    Just change numberposts=20 to whatever number you want to display.

    Hey John Pope, thanks a lot, exactly what I was searching for. A quick question: is there a way to exclude the current post from the list?

    Almost works, but not quite.

    This code (JohnPope) fails if it is inserted anywhere, but at the end of the post.

    Ex. I inserted it after the_content(), and what it does is show the comments for the last post listed by the code snippet.

    This is happening because the $post variable is set to the last post in the loop, and the main single page view’s loop is rendering the comments for the post in the $post variable, and not of the main post.

    Anyone knows how to get around it ?

    @neuville, check out the plugin that moshu has recommended. I just downloaded, evaluated and tested it.

    I looked into the code. The author is excluding the current post using it’s ID in the MySql query.

    So I suggest you try that plugin out, since it is already done and tested !

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘recent posts from the same category’ is closed to new replies.