How to Show Recent Posts by Category in WordPress

If you want to showcase a specific category in WordPress sidebar widget, you can choose one of two methods. First one is an easy method, and its by using a plugin called category posts widget.

To get started we’ll just install and activate the plugin, then go to Appearance — > Widget.

And drag the category  post widget where you want it to go.

From here you can give it a title,

choose which category you want to show,

and the number of posts to show for the category you can also choose how to sort it.

You have all these other options down here to choose from.

Just customize it to fit your needs and when you’re finished, click save.

Now we can go to our demo site and see it in action. Here, it’s showing the last 5 from my category like I want it to.

The second way to do this is a little bit more advanced it does not require a plugin, but it does require you to add some code to your theme’s function.php file.

First you want to make sure you backup your site. And then you add the code like this to your functions.php file. For the category slug make sure you put your category in there.

What this code does is it brings back the 10 posts from a specific category and displays in a bulleted list. .

Also, if the post has a featured image, then it will show that as well.

Down here, we made a shortcode and we’ve enabled it for widget use.

So now we can add this code to show our list on either a footer or a single.php file.

Or you can just add the text widget under Appearance — >Widgets,

And add the shortcode here.

You can see what it looks like on the demo site.

You’ll probably also need to add some css to make the list look better so test different css codes to tweak it to your liking. I hope you liked this article and found it helpful.

Оставьте комментарий

Ваш адрес email не будет опубликован.