Have you ever wanted to know how to display posts of the same category in WordPress?

This has already been the subject of several requests from our subscribers who wanted to prioritize a category in the widgets. In this tutorial then, I will show you how to display recent articles of a category in the widgets of your WordPress blog.

It exists two methods that will allow you to achieve your goals. The first method is very simple and suitable for beginner bloggers, because it is done through a plugin that will allow you to display recent posts from a category in a widget.

The second method is done with a piece of code that you can add in a file of your WordPress theme, but this method is only for experienced users.

The main advantage with this last method is that it does not make you dependent on a plugin, and you also have a lot of flexibility in the configurations. However, the method with the plugin is relatively simple, if you don't want to waste time, then I highly recommend it.

Well, as everything is said, I will present in the following the two methods.

But before, if you have never installed WordPress discover How to install a WordPress theme et How to search, install and activate a WordPress theme on your blog 

Then back to why we are here.

1st method: Using a "Category Post Widget" plugin

This is a Plugin available on WordPress, that you can install from your dashboard.

how to display the articles of a category-post-widget category

Once done, go to the interface " Appearance> Widgets And you will notice that a new widget is available.

You just have to move this widget and add it to the sidebar of your choice. Once it's done, you'll be able to choose a category, set an item limit to list, show or not the featured images (customizing the dimensions), define a limit in terms of words for categories.

See also How to add categories to the menu of your WordPress blog

category-widget

By consulting your blog, you will see how your widget is displayed. 

We also invite you to read How to give a unique style to different categories on WordPress

gallery-presentation- display the articles of the same category

2e method: Using a code

The only thing you will have to do is add the following code to the functions.php file of your active WordPress theme.

function custom _postsbycategory() {
// the query
$the_query = new WP_Query( array( 'category_name' => 'announcements', 'posts_per_page' => 10 ) );
// La boucle WordPress
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 {
                            // Si aucune image n’existe
                            $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
                            }
         }
} else {
         // Aucun article disponible
}
$string .= '</ul>';
return $string;
/* Restauration des données */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'custom_postsbycategory');
// Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');

Make sure you change the value of the key category_name ". You can increase the number of articles by using the key post_per_page ».

In a file of your theme you can use the following PHP function: " custom_postsbycategory () »

You can also discover How to add share buttons to WordPress by clicking on this link.

You can also use the shortcode " [Categoryposts] To display the list of items in a particular category in your articles, pages, or widgets.

Discover also some premium WordPress plugins  

You can use other WordPress plugins to give a modern appearance and to optimize the handling of your blog or website.

We offer you here some premium WordPress plugins that will help you do that.

1. Super Backup & Clone - Migrate

Le WordPress Plugin Super Backup & Clone – Migrate will allow you to backup your website. Indeed, working on your website is always a long and cumbersome process, and it would be a shame if you lost your work following a bad configuration or a manipulation error.

Super Backup Clone Migrate for WordPress

Ce WordPress Plugin is a trustworthy backup solution that will prevent you from losing your data. Its main features are: an intuitive dashboard, the possibility of keeping several instances of backups, migration to another possible server, restoration of backups, automatic diagnosis, excellent customer support, multisite support, etc…

Download | Demo | Web hosting

2. Captcha Plus

The Captcha plus plugin is one of the best WordPress security solutions to protect your forms from spam using mathematical logic. It supports login, registration, password recovery, comments and contact forms.

Captcha More

Users are required to complete the math equation before the form is submitted. You can configure the complexity of “Captcha Plus” using three basic math actions such as add, subtract and multiply. So, stop spamming now!

Download | DemoWeb hosting 

3. Premium SEO Pack

Most WordPress features are out of the box; this is arguably why WordPress is the most popular content management system in the world. Premium SEO Pack - WordPress Plugin

However, owners generally use WordPress in order to optimize the speed and SEO of their website. Premium SEO Pack is the ultimate tool for optimizing your website for search engines.

Discover more How to create a private store with WooCommerce

It has all the features you could want in dozens of different plugins, and bonus features that you will not see anywhere else.

Download | Demo | Web hosting

Recommended Resources

Find out about other recommended resources to help you build and manage your website.

Conclusion

Here ! That's it for this tutorial on adding recent articles from a specific category. Do not hesitate to share with your friends on your favorite social networks

However, you will also be able to consult our resources, if you need more elements to carry out your projects of creation of Internet sites, by consulting our guide on the WordPress blog creation.

If you have suggestions or remarks, leave them in our section Comments.

...