Skip to Main Content

How to display the posts of a category in WordPress

Divi: the easiest WordPress theme to use

Divi: The best WordPress theme of all time!

With over 901.000 downloads, Divi is the most popular WordPress theme in the world. It is complete, easy to use and comes with more than 62 free templates. [Recommended]

Have you ever wanted to know how to display the posts of a 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 help you achieve your goals. The first method is very simple and suitable for novice bloggers, because it is done through a plugin that will allow you to display recent articles of 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

Easily create your website with Elementor

Elementor allows you to easily create any website design with a professional look. Stop paying expensive for what you can do yourself. [Free]

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

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 look and optimize the grip of your blog or website.

Are you looking for the best WordPress themes and plugins?

Download the best plugins and WordPress themes on Envato and easily create your website. Already more than 49.720.000 downloads. [EXCLUSIVE]

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

1. Super Backup & Clone - Migrate

The WordPress Super Backup & Clone - Migrate plugin will allow you to back up 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 due to a bad configuration or a handling error.

Super Backup Clone Migrate for WordPress

This WordPress plugin is a trustworthy backup solution that will help you avoid losing your data. Its main features are: an intuitive dashboard, the ability to keep multiple instances of backups, migration to another possible server, restore backups, automatic diagnostics, 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 of the functionality of WordPress is already 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 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

Easily create your Online Store

Download free WooCommerce, the best e-commerce plugins to sell your physical and digital products on WordPress. [Recommended]

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.

... 

This article features 8 comments

  1. Hello,

    What should I replace the category_name with? By the slug, by the id…? can you put an example please?

    Goods.

  2. Hello,
    I warn anyone who wants to copy / paste the code. Replace all double and single quotes with their English version. example: "" by "". Be vigilant 😉

  3. Hello, and thank you for this very simple tutorial.
    I am a total beginner. What do you mean in step 2 by "in a specific file"? Should we create it or how to recognize it?

    1. Hello,
      I know the question was asked quite a while ago, but I am asking myself the same question as Carribou. What do you mean by "in a specific file"? it is said in a very mysterious way ...
      Thanks for the info

      1. Hello Michel,
        Thank you for your remark. We have corrected the article. It is "The only thing you will have to do is add the following code to the file functions.php of your active WordPress theme »

        But you can also create your own plugin and insert this piece of code into it.
        We hope we have answered your concern. Please do not hesitate to come back if you encounter any further difficulties.
        cordially

Leave a comment

Your email address will not be published. Required fields are marked with *

This site uses Akismet to reduce unwanted. Learn more about how your comments data is used.

Back To Top
20 shares
Share14
tweet2
Save4