Recent posts widget : how to display only the posts of the current category ?


Note :This snippet works only with the Classical style option. The Classical style option can be enabled in Appearance > Customize > Advanced > Theme Style. More about the theme style option here


If you want to display the recent posts of the current category archive, there is a simple solution.

You have to filter the query of the WordPress recent post widget. (one of the built-in widgets).

The filter takes one parameter : the query arguments array.

 

Here is the workflow :

1) in appearance > widget, drag and drop the recent posts widget in the wanted sidebar of the theme

2) copy/paste this code in your functions.php (of your child theme of course 😉 )

And that shoud do the trick. There are always useful filters in WordPress!

There is also a filter called ‘widget_title’ that could be filtered if you need to contextualized a bit more. Check out this snippet to filter the widget titles.

This filter is located in wp-includes/default-widgets.php.


Related Snippets you may also like


22 thoughts on “Recent posts widget : how to display only the posts of the current category ?

  • Ben James

    How can I add a categories filter in the widget settings form, so that I can set the category directly when I place the widget in the customizer?

  • Keith

    Hi This filter works like a dream. Thank you! Is it possible to create similar filters for the ‘Categories’, ‘Recent comments’, and ‘Archives’ widgets — so that each displays only the entries for the current category (in the case of the Categories widget, the child categories only)? I’m asking because I’m trying to divide my blog into two, using two basic categories, and I want the widgets on each of the two blogs (= basic category page) to display only the subcategories, posts, comments, and archives from that blog/category. Can this be done, please?

  • Mario

    Hello, thanks for sharing this.

    What if I want to use both of your codes? I mean, get_current_category if is_category() and get_current_categories if is_singular( ‘post’ )?

    • Nicolas Post author

      Hi Mario,
      You can add another conditional statement to the primary snippet with something loke that :

      Hope this helps!
      More about conditional statements in php

  • Jessie Gibson

    I was wondering there is a plugin or a way to customize this so that it shows the last post of a specific category but the featured image from that post? I have downloaded some sliders plugins but none of them let you just bring back a single category.

  • Derek

    Awesome, thanks!

    After posting my comment yesterday, I actually went away and tried it myself and this is what I came up with the following:

    This is working fine, but as you’ve said it only shows posts from one of the current categories. I decided I could live with that, but I’m going to update with your new code snippet now.

    Thanks for sharing!

  • Derek

    Hi Nicolas,

    Is there a way for this to pull the category of the current post? So that if a visitor is viewing a post page, the widget will get the category from the current post and display other posts from that category?

    Thanks,

    Derek

    • Nicolas Post author

      Hi Derek,
      The tricky thing to handle here is that you might have several categories assigned for a post.
      The first step would then be to get those categories, and then to add all post categories to the arguments in the snippet.
      1) get the categories of the current post :

      2) change the widget args :

      Hope this helps

      • Derek

        Awesome, thanks!

        After posting my comment yesterday, I actually went away and tried it myself and this is what I came up with the following:

        This is working fine, but as you’ve said it only shows posts from one of the current categories. I decided I could live with that, but I’m going to update with your new code snippet now.

        Thanks for sharing!

    • Ian

      Thank you. For the link. It looks perfect.
      The person I work for was a little spooked by the plugin on their server.
      I ended up adding short code in the functions file that fixed this.
      The solution can be found at wpbeginner, “How to Display a List of Child Pages For a Parent Page in WordPress”, & “how to add short code in your wordpress sidebar widget”.
      I’m a huge fan of Customizr! It has really helped me to become better at understanding wordpress.

  • Ian Mykel

    I am loving Customizr. It is easy to make changes and it looks so sharp, and I feel like a coder, even though I’m just plugging in fixes on my child theme.

    Not sure where to post this next question.

    I would like to know if there is a way for a page to show links in the sidebar which are specific for that page.To clarify, the page would have links in its sidebar that are different than links shown in the sidebar on a different page (or post).

    It would make sense to have a widget look for information from a custom field to display, such as a previously created menu, or a particular link category. That way the custom fields in the screen-options for a page (or post) would allow the admin to select which menu, or links, to show in the side bar while on the page they are creating. It sounds like a widget fix, but I’m not sure.

    Do you have any ideas that can help me achieve the goal of adding links in a sidebar that are specific for that page?

    I’m working on a project that isn’t live yet.

  • Joshua Sherman

    Hi Nicolas! Thanks so much for your help.

    I want to show recent posts, filtered by tags and categories, on pages that contain text.

    Right now I’m using a plugin called “list category posts” to pulls posts from a category, and into my page (see it at the bottom of http://leichtag.quexion.net/the-ranch/) It’s pretty ugly.

    I’d rather use your beautiful posts pages. Anyway of displaying posts on a specific page, maybe thru a shortcode?

    Thanks Nicolas!!

Comments are closed.