In this article:
- Create a Recent Posts by Category Widget
- Approach 1: Use Recent Post Widget Extended Plugin
- Approach 2: Use Display Posts Plugin
- Using Widget Logic to Display Recent Posts per Category
- Display Recent Posts for Current Category
- Step by Step Instructions
- Ready to Try?
- Test Post Widget Extended + Widget Logic
- Test Display Posts + Widget Logic
The WordPress Core Recent Posts Widget is an easy way to display the most recent posts to your visitors. Unfortunately there isn’t a way to only show recent posts from a specific category (or categories) without the use of a plugin.
To find a solution, we did some testing using the Recent Posts Widget Extended and Widget Logic plugins. While they do not work with themes using the new block widget interface like Twenty Twenty-One, they do work well together in other WordPress themes.
If you are using Twenty Twenty-One, you should consider using the Display Posts plugin to accomplish the same goal. Since this plugin uses shortcodes instead of a visual interface, it will work with Twenty Twenty-One.
Each plugin will be discussed in greater detail below and an example of how they can work together to give you greater control over what posts your users see in widgets.
Please note that these plugins have a warning that it hasn’t been tested with the most recent versions of WordPress. We strongly advise you test this plugin in a WordPress staging environment with your theme and current plugins to ensure compatibility.
Create a Recent Posts by Category Widget
Approach 1: Use Recent Post Widget Extended Plugin
The Recent Posts Widget Extended plugin allows you to create a recent post list with posts only from a single category. You can also use the plugin to create other types of post lists (recent or not) using the available settings. It adds a new widget to your dashboard.
- Navigate to Appearance > Widgets.
- Select Recent Posts Extended from the widget list.
- Add the widget to the desired widget location, which will vary by theme.
- You can leave most settings as they are, but be sure to look at these:
- Number of posts to show
- Display Thumbnail
- Display Readmore
- In the Limit to Category box, check the category or categories you’d like to display posts from.
- Navigate to the front end of your website to see your new widget!
As shown above, there are many options you can use to customize each Recent Post list. Feel free to experiment with different options to create unique post lists.
Approach 2: Use Display Posts Plugin
You can also use the Display Posts plugin to create a post list by category. You will use a shortcode to choose what displays – excerpt, image, read more link, etc – using the instructions on the Display Posts plugin page on WordPress.org.
The basic shortcode is
To only show the posts from our Favorites category, we will need to add a parameter. The full list of parameters that can be used is on the plugin’s website.
- Navigate to Appearance > Widgets.
- Add a Text Widget.
- Paste your desired shortcode in the widget.
The shortcode we used is:
[display-posts include_excerpt="true" excerpt_length="20" image_size="large" wrapper="div" wrapper_class="display-posts-listing image-top" category="favorites"]
This shortcode will display a 20 characters excerpt of each post, shows the featured image above the post content, and will only show posts in the category Favorites. On the front end, it looks like this:
Using Widget Logic to Display Recent Posts per Category
Either widget we just created will show everywhere the widget area is active. If you’d like to limit where the widget displays, you can add the Widget Logic plugin. It adds an extra field at the bottom of every widget, like shown below.
The text field lets you use any of WordPress Core’s Conditional Tags, or any general PHP code, to select exactly where the widget is shown on your website. For example:
is_home()— only on the main blog page
is_page('about')— only on the About page
in_category('favorites')— only on the Favorites category page and posts
in_category( array('cat-1','cat-2') )— only on the Cat-1 and Cat-2 category pages and posts
Display Recent Posts for Current Category
Now we have a way to display Recent Posts from a specific category, and we know how to display a widget only where we want. Using the plugins together allows you to show Recent Posts specific to the category the user is in, without creating multiple sidebars or using custom code in Core files.
For example, let’s say you have a category called Favorites. When a visitor is reading an article in the Favorites category you only want to show a Recent Post list with posts from the Favorites category. When the user changes to a different category, the Recent Post list will change to only show posts from that new category.
Step by Step Instructions
You will need to use either method to create multiple Recent Post widgets – one for each category you want Recent Posts to show on. Add them all to the same widget area, like the Primary Sidebar. Each Recent Post widget needs to include logic in the last input box to ensure the widget only displays where you want it to, otherwise it will display everywhere.
- Create a Recent Posts widget with the desired category selected, like shown in the first part of this article.
in_category('favorites')to the Widget Logic input at the bottom of the widget panel, replacing favorites with your category name.
- View the front end of your site. You should only see the Recent Post list when you are in the category entered in Widget logic, and not anywhere else.
- Create another Recent Posts widget with a different category checked.
in_category('news')to the Widget Logic input at the bottom of the widget panel, replacing news with your category name.
- Add it to the same widget area.
- View the front end of your site again. Switch between posts in the two categories to see the post list change.
You can repeat this process for as many categories as you need, there’s no limit on how many you can create.
Ready to Try?
As mentioned previously we strongly recommend you try these plugins in a staging environment to be sure they work with your theme and plugins, and to ensure you have a good understanding of the logic code before saving to a live site.
Test Post Widget Extended + Widget Logic
Test Display Posts + Widget Logic
If you’d prefer to use shortcodes, you can test Display Posts and Widget Logic instead.
We hope you found this tutorial helpful and can use this information to gain greater control over your WordPress website. Let us know below if you have any questions.
200+ Design Templates + 1 Kick-ass SuperTheme
6 WordPress Plugins + 2 Essential Services
Everything you need to build and manage WordPress websites in one Central place.