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.
Video Tutorial
If you don’t like the video or need more instructions, then continue reading.
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.
function wpb_postsbycategory() { // the query $the_query = new WP_Query( array( 'category_name' => 'announcements', 'posts_per_page' => 10 ) ); // The Loop if ( $the_query->have_posts() ) { $string .= '<ul class="postsbycategory widget_recent_entries">'; while ( $the_query->have_posts() ) { $the_query->the_post(); if ( has_post_thumbnail() ) { $string .= '<li>'; $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>'; } else { // if no featured image is found $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>'; } } } else { // no posts found } $string .= '</ul>'; return $string; /* Restore original Post Data */ wp_reset_postdata(); } // Add a shortcode add_shortcode('categoryposts', 'wpb_postsbycategory'); // Enable shortcodes in text widgets add_filter('widget_text', 'do_shortcode');
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).
<?php wpb_postsbycategory() ?>
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.
ul.postsbycategory { 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+.
If i have 10 categories, do i just paste the code snippet 10 times within functions.php
and
alter the ‘announcements’ for each of my slug for their category
and
Alter the “categoryposts” name for each code snippet so the shortcode knows which category posts should be displayed for which category
I know its a few years old now but if be greta to know if this works
We have an updated article on this that you would want to take a look at below:
https://www.wpbeginner.com/wp-tutorials/how-to-display-recent-posts-from-a-specific-category-in-wordpress/
Thank you so much for this tutorial. I worked perfectly as described. What if I want it to show the posts, not just links to the posts?
I don’t know why $string didn’t work for me so I replace it for echo
Thanks so much! this was exactly what I was looking for. Now my Blog looks more attractive. i love wpbeginner.com keep it up….
Hey Emeka,
Thanks for the kind words
Don’t forget to join us on Twitter for more WordPress tips and tutorials.
Hi, thanks for this! It’s exactly what I was looking for. When I pull in a post from a category anywhere on the page, it changes the content of the original post on the page. Any ideas why this is happening? I’m trying to place a featured posts in a sidebar and place this sidebar on the single post pages. Thanks for any help!
can i change shorcode name as I wise…?
can I create this every category…?
please tell me….
Thanks for this post! How would I go about adding excerpts to the code snippet?
This is great. The posts by category worked perfectly. One question: Is there a way for it to display the posts in the category without showing an entry for the current post. For example, lets say there’s a category for cute animals and the current post is about cute puppies, is there a way to exclude the cute puppies link from the list of related posts? Hope that makes sense. thanks
Hi Debra, if you’re still struggling with this you can amend the query slightly. I have done the same and it is working well. Just adjust the code to be the following –
$current_id = get_the_ID();
$the_query = new WP_Query( array( ‘category_name’ => ‘services’, ‘posts_per_page’ => 10, ‘post__not_in’ => array($current_id) ) );
Hope that helps.
Rob
Thanks for your code. I have used it in y site. It works well to display my post in my wall.
Thanks, it works great.
I’ve tried to add pagination to the script like this:
$paged = ( get_query_var(‘paged’) ) ? get_query_var(‘paged’) : 1;
$query_args = array(
‘post_type’ => ‘post’,
‘category_name’ => ‘actueel’,
‘posts_per_page’ => 3,
‘paged’ => $paged
);
$the_query = new WP_Query( $query_args );
Could you help me finish this code to make it work?
Kind regards,
Debby
Hi,
Great tutorial and it works but I need to get the titles to show under the thumbnails, so do you have any good or hint how to do this in CSS?
Thanks!
If you are using the code method then you can simply display thumbnail before post title or excerpt.
hi,
thanks for this its great! i want to also include the post meta. how would go about also displaying this?
div.post-meta
Looks good.. i wil try it
On this page there is a section “More on wpbeginners now”
looks the same but now with two colums… how can I achieve this layout ??
In the code version, this bit of code cannot be reached because it comes after a return statement:
/* Restore original Post Data */
wp_reset_postdata();
Great stuff! Works perfectly, exactly what I needed. I don’t like adding more plugins, because I have so many already, so the function code was very useful. Thanks so much!
i try this code to add in functions.php but it did not work 4 me i have constructo Themes
Thanks. This is exactly waht i was looking for. Very useful!
Nice post. It was working…
Great! Thanks a lot!
is it possible show multi categories slug with Code Snippet ?
Works perfectly but I think if your are using the first method ; you need to echo the function else you get no results.
What part of the code do I need to delete to hide the images?
I do not want to use display:none; in the CSS because the function appears to be still fetching the image.
I just want to stop the PHP fetching the thumbnail altogether and just show titles.
Thanks.
You can remove get_the_post_thumbnail() portion to stop pulling out the thumbnail image per post.
Very nice example. Thank you.
Could you also add an attribute to the shortcode to enable the selection of the category in the widget? Instead of the hard-coded “announcemnts”. Such that I can use different widgets on different locations showing different categories?
Hello, I can’t seem to get this to work in my site-specific plugin. I already have some code snippets there and they work perfectly, but adding the above code immediately disabled the plugin. Please what am I missing? …and thanks as usual for your very helpful tutorials.
Thanks for the suggestion, duly noted.
Glossary is a custom post type.
hi i’m new to wordpress, thanks to wpbeginner for teaching us for free. i wanna know how can we create menu permalink like ur page url https://www.wpbeginner.com/glossary/permalinks/ (glossary/permalinks). is glossary a post or category or page. can u pls explain this
Excellent article .
If you could write about some plugins or widgets to display categories with subcategories (rather than all kinds waterfall look ) or display with a drop-down menu, but from the sidebar , would be very grateful .
Funny, I literally did a search on your site for this last night. Glad to see you have the same plugin recommendation as your last article on this. I used it and it works great. Thank you.