How to Display Recent Posts From A Specific Category In WordPress

Do you want to display recent posts from a specific category in WordPress? The default recent posts widget shows posts from all categories, and there is no option to filter them by category. In this article, we will show you how to easily display recent… Read More »

The post How to Display Recent Posts From A Specific Category In WordPress appeared first on WPBeginner.

Do you want to display recent posts from a specific category in WordPress? The default recent posts widget shows posts from all categories, and there is no option to filter them by category. In this article, we will show you how to easily display recent posts from a specific category in WordPress.

Display recent posts from specific category in WordPress

Filtering Posts by Category in WordPress

If you just want to create a page to display recent posts from a particular category, then your WordPress site already has separate pages for each category.

You can add links to all your category pages by visiting Appearance » Widgets page and adding the ‘Categories’ widget to your sidebar. You can also add categories in your navigation menus.

On the other hand, if you want to show recent posts from a specific category in your sidebar, then there is no default widget for that. The default recent posts widget does not allow you to filter posts by category or tags.

Thankfully there is another way. Let’s take a look at how to easily display recent posts from specific category in WordPress.

Method 1. Show Recent Posts from a Category Using Plugin

This method is easier, and it is recommended for most users.

First thing you need to do is install and activate the Recent Posts Widget Extended plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, you need to visit the Appearance » Widgets page and add ‘Recent Posts Extended’ widget to your sidebar.

Limit recent posts by category

The widget menu will expand to show its settings. You need to select the category or categories that you want to display under the ‘Limit to Category’ option.

The widget comes with a lot of options that you can customize. You can show post thumbnail, date, relative date, post summary / excerpts, and more.

Don’t forget to click on the save button to store your widget settings.

You can now visit your website to see the recent posts displayed by category.

Recent posts from specific category

Display Recent Posts by Category Using Shortcode

The Recent Posts Extended Widget also allows you to use shortcode to display recent posts anywhere on your site including posts and pages.

You will need to edit the post or page where you want to display the recent posts from a specific category. In the post editor, you will need to add the following shortcode:

[rpwe limit="5" excerpt="true" cat="72" ]

This shortcode displays 5 recent posts from a specific category with the post excerpt. You will need to replace the cat value with the ID of the category that you want to display. See our article on how to find category ID in WordPress.

After adding the shortcode, you can save your post or page to view your changes.

Posts by category displayed using shortcode

Method 2. Display Recent Posts From Specific Category using Code Snippet

This method requires you to add code to your WordPress theme files. If you haven’t done this before, then take a look at our guide on how to copy and paste code in WordPress.

You will need to add the following code in your WordPress theme files where you want to display recent posts from a specific category.

<?php $catquery = new WP_Query( 'cat=72&posts_per_page=5' ); ?>
<ul>

<?php while($catquery->have_posts()) : $catquery->the_post(); ?>

<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile;
	wp_reset_postdata();
?>

The first line of this code creates a new WordPress query with a specific category ID. You need to replace it with your own category ID. It only shows post title in a list.

You can change it to display full content by adding the following code:

<?php $catquery = new WP_Query( 'cat=72&posts_per_page=5' ); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
<?php endwhile; ?> 
</ul>
<?php wp_reset_postdata(); ?>

You can also replace the the_content with the_excerpt to display post excerpts instead of full article.

We hope this article showed you how to easily display recent posts from a specific category in WordPress. You may also want to see our list of 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 Facebook.

The post How to Display Recent Posts From A Specific Category In WordPress appeared first on WPBeginner.

How to Display Recent Posts From A Specific Category In WordPress

Do you want to display recent posts from a specific category in WordPress? The default recent posts widget shows posts from all categories, and there is no option to filter them by category. In this article, we will show you how to easily display recent… Read More »

The post How to Display Recent Posts From A Specific Category In WordPress appeared first on WPBeginner.

Do you want to display recent posts from a specific category in WordPress? The default recent posts widget shows posts from all categories, and there is no option to filter them by category. In this article, we will show you how to easily display recent posts from a specific category in WordPress.

Display recent posts from specific category in WordPress

Filtering Posts by Category in WordPress

If you just want to create a page to display recent posts from a particular category, then your WordPress site already has separate pages for each category.

You can add links to all your category pages by visiting Appearance » Widgets page and adding the ‘Categories’ widget to your sidebar. You can also add categories in your navigation menus.

On the other hand, if you want to show recent posts from a specific category in your sidebar, then there is no default widget for that. The default recent posts widget does not allow you to filter posts by category or tags.

Thankfully there is another way. Let’s take a look at how to easily display recent posts from specific category in WordPress.

Method 1. Show Recent Posts from a Category Using Plugin

This method is easier, and it is recommended for most users.

First thing you need to do is install and activate the Recent Posts Widget Extended plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, you need to visit the Appearance » Widgets page and add ‘Recent Posts Extended’ widget to your sidebar.

Limit recent posts by category

The widget menu will expand to show its settings. You need to select the category or categories that you want to display under the ‘Limit to Category’ option.

The widget comes with a lot of options that you can customize. You can show post thumbnail, date, relative date, post summary / excerpts, and more.

Don’t forget to click on the save button to store your widget settings.

You can now visit your website to see the recent posts displayed by category.

Recent posts from specific category

Display Recent Posts by Category Using Shortcode

The Recent Posts Extended Widget also allows you to use shortcode to display recent posts anywhere on your site including posts and pages.

You will need to edit the post or page where you want to display the recent posts from a specific category. In the post editor, you will need to add the following shortcode:

[rpwe limit="5" excerpt="true" cat="72" ]

This shortcode displays 5 recent posts from a specific category with the post excerpt. You will need to replace the cat value with the ID of the category that you want to display. See our article on how to find category ID in WordPress.

After adding the shortcode, you can save your post or page to view your changes.

Posts by category displayed using shortcode

Method 2. Display Recent Posts From Specific Category using Code Snippet

This method requires you to add code to your WordPress theme files. If you haven’t done this before, then take a look at our guide on how to copy and paste code in WordPress.

You will need to add the following code in your WordPress theme files where you want to display recent posts from a specific category.

<?php $catquery = new WP_Query( 'cat=72&posts_per_page=5' ); ?>
<ul>

<?php while($catquery->have_posts()) : $catquery->the_post(); ?>

<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile;
	wp_reset_postdata();
?>

The first line of this code creates a new WordPress query with a specific category ID. You need to replace it with your own category ID. It only shows post title in a list.

You can change it to display full content by adding the following code:

<?php $catquery = new WP_Query( 'cat=72&posts_per_page=5' ); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
<?php endwhile; ?> 
</ul>
<?php wp_reset_postdata(); ?>

You can also replace the the_content with the_excerpt to display post excerpts instead of full article.

We hope this article showed you how to easily display recent posts from a specific category in WordPress. You may also want to see our list of 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 Facebook.

The post How to Display Recent Posts From A Specific Category In WordPress appeared first on WPBeginner.

How to Show Recent Posts by Category in WordPress

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… Read More »

To leave a comment please visit How to Show Recent Posts by Category in WordPress on WPBeginner.

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.

Posts by Category

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.

Category posts widget settings

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;
}

Posts from a category displayed with thumbnails

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+.

To leave a comment please visit How to Show Recent Posts by Category in WordPress on WPBeginner.