Have you ever wanted to display your recent posts from each category in the 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 see how to display recent posts by category in the WordPress sidebar.
If you don't like the video or need more instructions, continue reading.
There are two ways to display recent posts by category in WordPress. The first method is quite simple and beginner friendly because we will use a plugin to display recent posts by category in a widget (no coding required).
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 of using the code method is that it doesn't depend on a plugin, and it has a few more customization options. However, the plugin method is EASY and has the most customization options to satisfy 95% of people, such as displaying post thumbnails, displaying post excerpts and controlling excerpt duration, displaying date of publication and the number of comments, etc..
With that said, let's see how you can display recent posts by category in the WordPress sidebar with the Category Post Widget plugin.
The 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 the Category Posts widget into a sidebar where you want to display recent posts by category.
The widget options are pretty self explanatory. You must first 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 images, etc.
Once you're done, click the Save button to store your widget settings. You can now visit your site to see recent posts by category in action.
In this method, we'll use a code snippet to display recent posts from a category.
You need to add this code first in your theme's functions.php file or in a site-specific plugin.
función wpb_postsbycategory () // the query $ the_query = new WP_Query (array ('category_name' => 'advertements', 'posts_per_page' => 10)); // The Loop if ($ the_query-> have_posts ()) $ string. = '
Make sure to replace
'anuncios' with your own category slug.
This code simply queries WordPress to retrieve 10 posts from a specific category. It then displays the messages in a bulleted list. If a post has a featured image (post thumbnail), it will also display the featured image.
At the end, we create a shortcode.
'categoríaposts' and enabled shortcode in text widgets.
There are three ways to display recent posts by category using this code snippet.
First, you can paste the following code anywhere in the template file location you like (such as footer.php, single.php, etc.).
The second and third methods are based on using the shortcode in the widget area or within 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 display recent posts by categories on specific posts or pages, just 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 post list. You can use the code below as a starting point in your theme or child theme's style sheet.
ul.postsbycategory list-style-type: none; .postsbycategory img float: left; relleno: 3px; margen: 3px; borde: 3px sólido #EEE;
That's it, we hope this article helped you to 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 enjoyed this article, please subscribe to our WordPress YouTube Channel video tutorials. You can also find us on Twitter and Google.+.