How to display recent posts from a specific category in WordPress

recent posts from specific category

How to display recent posts from a specific category in WordPress

Many WordPress themes comes with recent posts feature by default. In that, some themes display only two recent posts while some themes display four recent posts and it all depends on the theme you install in your blog. But most of the WordPress themes which offer recent posts feature, display recent posts randomly or display posts from the same category. What if you want to display recent posts from a specific category in your WordPress blog?

Today, I am going to show you how to display recent posts from a specific category

If your theme has already recent posts feature, then search for the following code in single.php if you’re recent posts are appearing below your posts or search in sidebar.php if your recent posts are appearing in your sidebar.

<h2>Recent Posts</h2>

<ul>

<?php get_archives(‘postbypost’, 10); ?>

</ul>

Now, before you edit any of your WordPress files, make sure you backup them first, so that incase if anything goes wrong means, you will always have your original file with you.

Now,

Replace the above code with the below code

<ul>

<?php $recent = new WP_Query(“cat=1&showposts=10″); while($recent->have_posts()) : $recent->the_post();?>

<li><a href=”<?php the_permalink() ?>” rel=”bookmark”>

<?php the_title(); ?>

</a></li>

<?php endwhile; ?>

</ul>

Now, in the above code, cat=1 means to display posts from category id 1

If you want to know how to see category id of your desired category, see below

Click on Categories in your posts tab in your WordPress admin panel

recent posts

Now choose your desired category and click on Edit

recent posts

In your address bar, you can able to see the category id of your chosen category. In my case, my category id is 28.

See below for screenshot

recent posts

Now, you got your category id. To display recent posts from your desired category, replace cat=1 to cat=your category number.

In my case, I would replace it with cat=28

You can also adjust the number of recent posts which are going to display by editing showposts=10 in the above code. Currently it was set to show 10 recent posts, if you want to increase or decrease the number of recent posts displayed, just change the number to your desired one.

Now, if you already don’t have recent posts feature in your theme, then follow the steps on how to show recent posts from a specific category.

Choose the position where you want to display the recent posts from specific category. For example, if you want to display the recent posts on your single.php (Single Post page) paste the below code on the location where you want to display your recent posts.

<ul>

<?php $recent = new WP_Query(“cat=1&showposts=10″); while($recent->have_posts()) : $recent->the_post();?>

<li><a href=”<?php the_permalink() ?>” rel=”bookmark”>

<?php the_title(); ?>

</a></li>

<?php endwhile; ?>

</ul>

You can do the same thing, if you want to display your recent posts in sidebar too.

You can also display recent posts using plugins, see

How to add related posts after blog posts in WordPress

Let me know if you have questions regarding this…