Have you ever wanted to showcase your recent posts from each category in your WordPress sidebar? Recently, one of our users asked us for an easy way to display recent posts from a specific category in WordPress sidebar widgets. In this article, we will cover how to show recent posts by category in your WordPress sidebar.
There are two ways to display recent posts by category in WordPress. The first method is fairly simple and beginner friendly because we will use a plugin to display recent posts by category in a widget (no coding necessary).
The second method uses a code snippet for our advanced DIY users, so you can display recent posts from a specific category without a plugin.
The only advantage to using the code method is that you are not dependent on a plugin, and you have a few more customization options. However the plugin method is EASY and has most of the customization options to satisfy 95% of the people such as show post thumbnail images, display post excerpt and control excerpt length, show the post date and number of comments, etc.
Having that said, letβs take a look how you can can show recent posts by category in your WordPress sidebar with the category post widget plugin.
Display Recent Posts by Category (Plugin Method)
First thing you need to do is install and activate the Category Posts Widget plugin.
Upon activation, you need to visit Appearance Β» Widgets, there you will notice the new Category Posts widget in the list of available widgets.
Simply drag and drop Category Posts widget to a sidebar where you want to display recent posts by category.
The widget options are quite self explanatory. First you need to provide a title for the category posts section and choose a category. After that you can choose other display options like number of posts, excerpts, featured image, etc.
Once you are done, click the save button to store your widget settings. You can now visit your site to see recent posts by category in action.
Display Recent Posts by Category without a Plugin (Code Snippet)
In this method, we will use a code snippet to display recent posts from a category.
First you need to add this code in your themeβs functions.php file or a site-specific plugin.
Make sure that you replace ‘announcements’ with your own category slug.
This code simply queries WordPress to retrieve 10 posts from a specified category. It then displays the posts in a bulleted list. If a post has a featured image (post thumbnail), then it will show the featured image as well.
In the end, we created a shortcode ‘categoryposts’
and enabled shortcode in text widgets.
There are three ways of displaying the recent posts by category using this code snippet.
First, you can simply paste the following code anywhere in your desired template file location (such as footer.php, single.php, etc).
Second and third method relies on using the shortcode in the widget area or inside your posts / pages.
Simply visit Appearance Β» Widgets and add a text widget to your sidebar. Next add [categoryposts] shortcode in the text widget and save it. You can now preview your website to see recent posts by category in the sidebar.
If you want to show recent posts by categories on specific post or pages, then simply paste the shortcode in the post content area.
By default, your list may not look very good. You will need to use CSS to style the category posts list. You can use the code below as an starting point in your theme or child themeβs stylesheet.
list-style-type: none;
}
.postsbycategory img {
float:left;
padding:3px;
margin:3px;
border: 3px solid #EEE;
}
Thatβs all, we hope this article helped you display recent posts by category in WordPress sidebar. You may also want to check out these most wanted category hacks and plugins for WordPress.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.