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 😉 )
add_filter( 'widget_posts_args', 'my_widget_posts_args'); function my_widget_posts_args($args) { if ( is_category() ) { //adds the category parameter in the query if we display a category $cat = get_queried_object(); return array( 'posts_per_page' => 10,//set the number you want here 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'cat' => $cat -> term_id//the current category id ); } else {//keeps the normal behaviour if we are not in category context return $args; } }
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.
23 thoughts on “Recent posts widget : how to display only the posts of the current category ?”
Hi,
You can use one of the several plugins available for this purpose. For example
Recent Posts by Category Widget
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?
Wuooooo Great. Thank you very much. Its’ working well for me
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?
Hi keith, you can of course modify every default WordPress widgets with the hook API.
Check the default widget code to find the filter you need for your widgets : https://core.trac.wordpress.org/browser/tags/3.9/src/wp-includes/default-widgets.php.
Hope this helps
Thanks for your help, Nicolas!
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’ )?
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
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.
Hi Jessie, I don’t know any plugin doing that. However, I have found this snippet that might be useful.
Hope this helps
Yeah that helps a bunch. Thank you.
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!
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
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
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!
Hi Ian, the plugin you are looking for to do this is called widget logic : http://wordpress.org/plugins/widget-logic/
I am sure you are gonna love it!
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.
Thanks for the feedback!
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.
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!!
Thank you so much, Nicolas!
This is really helpful for me ^^
You’re welcome, happy if it can help!
Enjoy the theme