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.
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
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
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.
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.
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 | Demo | Web 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.
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.
- 8 WordPress plugins to create a megamenu on your blog
- 10 WordPress plugins to hide premium content on your blog
- How to easily create a Quiz in WordPress
- How to create a privacy policy on WordPress
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.
...
Hi, I don't understand this part:
In a file of your theme you can use the following PHP function: "custom_postsbycategory()"
Code won't register because syntax error, unexpected '_postsbycategory' (T_STRING), expecting '('
Do you have a solution to unblock the situation please?
Hi,
No better solution yet. Sorry.
Hello,
What should I replace the category_name with? By the slug, by the id…? can you put an example please?
Goods.
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 😉
Thank you for the precision, we will correct.
Thanks for the return Alex
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?
Hello,
If you are a beginner, so I not recommend the second method.
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
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