How to Display Recent Posts From A Specific Category In WordPress
Sometimes we want to display posts only from a specific category. This can be added to the sidebar or footer to show posts from a category. There can be various reasons for this. By default, WordPress does not allow this, but there are a few ways by which you can add this. Most of the modern themes now come with recent post widgets, but very rare themes offer recent posts by a specific category. If your WordPress theme does not offer this, you can try the methods given below to add recent posts from a specific category.
Best Managed WordPress HostingWPEngine is the best and most secure managed hosting provider
Display Recent Posts From A Specific Category In WordPress
These are the main ways to display recent posts from a specific category.
- Category Post Widget
- By Editing theme code
Category Post Widget:
This is the simplest way of adding posts from a specified category. Category Posts Widget is a nice WordPress plugin that allows users to add a list of posts from a category on any of the widgets available. This also has some customizing options from where the user can set a number of posts to display, thumbnails, and many other things. The user can add as many widgets as he wants and can also set for different categories.
By Editing Theme code:
If you are a PHP programmer and have some understanding of PHP themes, you can also follow this manual method of adding posts by category.
Use this code and add it to the sidebar.php file of the WordPress theme or any other place where you want to show the posts.
<?php query_posts('posts_per_page=1&cat=1'); while(have_posts()) : the_post(); ?> <ul> <li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3> <ul><li><?php the_content(); ?></li> </ul> </li> </ul> <?php endwhile; ?>
Change the Cat=1. Here 1 is the id of the category from which we want to display the posts. Change this id to pick which category will you be displaying.
If you do not know how to get the category id to use in the code. Go to Posts -> Categories and look for the category you want to use. Below the category, you will see the link to Edit. Click on it and open the edit page of the category. Now see the URL and you will get the parameter tag_ID=value. here the value is the category id.
For example, this is the edit URL of a category on my site: https://thewpguides.com/wp-admin/term.php?taxonomy=category&tag_ID=48&post_type=post
Here the category id is 48.
To find out the category id and use it in the code.
Now you know how to display recent posts by category on your WordPress site. If you still face any problems or want to say something, you can comment below. If you have something to add to the story or suggest something, I will be happy to see your comment.