WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] recent posts from the same category (18 posts)

  1. moshu
    Member
    Posted 6 years ago #

    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!

  2. SimonJ
    Member
    Posted 6 years ago #

    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') ): ?>
    RSS FEED OF LAST 5 POSTS FROM CATEGORY 1
    <?php endif; ?>
    
    <?php if ( in_category('2') ): ?>
    RSS FEED OF LAST 5 POSTS FROM 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... :-)

    S.

  3. moshu
    Member
    Posted 6 years ago #

    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)

  4. Chris_K
    Member
    Posted 6 years ago #

    You'd use get_posts, right?

    $myposts = get_posts('numberposts=x&category=y')

    Then loop through 'em for display.

  5. moshu
    Member
    Posted 6 years ago #

    category=y
    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...

  6. SimonJ
    Member
    Posted 6 years ago #

    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 :

    http://www.dagondesign.com/articles/other-posts-from-cat-plugin-for-wordpress/

    S.

  7. moshu
    Member
    Posted 6 years ago #

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

  8. moshu
    Member
    Posted 6 years ago #

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

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

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

  9. SimonJ
    Member
    Posted 6 years ago #

    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... ;-)

    S.

  10. moshu
    Member
    Posted 6 years ago #

    Check the post above yours - it works for me!

  11. Chris_K
    Member
    Posted 6 years ago #

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

    <?php
    $cat = get_the_category(); $cat = $cat[0];
    ?>

    Would get you the first category assigned.

  12. moshu
    Member
    Posted 6 years ago #

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

  13. JohnPope
    Member
    Posted 5 years ago #

    You can do it with this bit of code:

    <?php
    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.

  14. neuville
    Member
    Posted 5 years ago #

    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?

  15. theblogdoctor
    Member
    Posted 5 years ago #

    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 ?

  16. theblogdoctor
    Member
    Posted 5 years ago #

    @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 !

  17. johndeere
    Member
    Posted 5 years ago #

    the plugin works great by the way although I had not exactly the same problem... thank you guys! :)

  18. Ionut
    Member
    Posted 5 years ago #

    Hi, I have to use JohnPope's idea to add excerpt and thumbnails to that list, BUT the problem is that this code below just show other posts from the first category this post is found in. I need it changed to check all categories that current post is in and display the latest post found in any of these categories. Anybody know how to do that ? Pretty pliz

    `<?php
    global $post;
    $categories = get_the_category();
    $category = $categories[0];
    $cat_ID = $category->cat_ID;
    $myposts = get_posts("numberposts=8&category=$cat_ID");
    ?>
    <?php foreach($myposts as $post) :?>
    "><?php the_title(); ?>
    <?php the_thumb(); ?><?php the_excerpt(__('Read More'));?> " href="<?php the_permalink(); ?>">
    
    <?php endforeach; ?>
    

Topic Closed

This topic has been closed to new replies.

About this Topic