How to Display Posts from a Specific Category in WordPress
In your WordPress website, few things will disrupt the flow of a website’s surfing experience like having to navigate through a large list of unconnected posts.
Then, instead of continuing to the next page, your user can hit the close button and navigate to a different website.
Therefore, finding an efficient way to display posts from category WordPress is a must.
In this post, Arrowtheme will deliver you a comprehensive guide to display posts from a specific category in WordPress through some outstanding methods. So, let’s explore with us right now!
Why should you filter posts by category in WordPress?
In WordPress, users are able to discover new material as they surf your website by displaying recent posts.
In fact, this significantly boosts the traffic of your blog and page views. Besides, you might wish to present recent postings relevant to the content users are now watching with the goal of making it even more useful.
Moreover, visitors can stay longer on your website thanks to this easy step, resulting in increasing more email subscribers and sales.
Specifically, if you want to show all of your posts from a particular category on a separate page, you just need to first go to Posts > Categories > View page.
Then, you must click on the “View” link underneath a category to access the category page.
How to add recent posts by category by using Block Editor?
The latest posts block is included in the block editor by default in WordPress. It enables you to quickly see recent posts within a page or post, as well as filter them by category.
First of all, you have to update the post or page where you wish the latest posts by category to appear. After that, you must click the add new block button (+) on the post edit page, then look for the “latest postings” block.
Then, the block with a preview of your recent posts will appear in the content section. Following that, you just simply need to click the block to pick it, and the block settings will appear in the right column.
In particular, you will find options like showcasing the featured image, post author, date, and text at the top of the settings.
After that, under the block settings, you need to scroll down to the Sorting and filtering area. Here, you must specify the name of the category from which you wish to display postings.
Moreover, you can also select multiple categories with the help of a comma to separate the names of the categories.
Then, you will see that the preview of your most recent posts has changed to only show posts from the categories you have selected.
What’s more, you can now save and preview your post or page in a browser window.
How to add recent posts by category in WordPress sidebar?
Users who wish to display posts from category WordPress in their sidebar should take a look at this method. For more details, you must first install the Recent Posts Widget With Thumbnails plugin and activate it.
To do so, you have to go to Appearance > Widgets and add the “Recent Posts with Thumbnails” to your sidebar after activating it.
In addition, this widget has a lot of customization options that you may look over and tweak to fit your specific needs.
To begin, let’s give your widget a title and then scroll down to the “Filter by category” section.
After that, you may choose which categories to display posts from using this menu.
Following that, you may also pick multiple categories by holding down the CTRL key on your keyboard and clicking on the categories you want to select. Then, in order to save your widget settings, you need to click the “Save” button.
Way to add recent post from a specific category in WordPress by using code
Another solution you can’t ignore to display posts from category WordPress is to utilize code. To begin, you must copy and paste the following code into the functions.php file of your WordPress theme or a site-specific plugin.
1 function wpb_postsbycategory() {
2 // the query
3 $the_query = new WP_Query( array(
4 'category_name' => 'news',
5 'posts_per_page' => 5
6 ) );
7
8 // The Loop
9 if ( $the_query->have_posts() ) {
10 $string .= '<ul class="postsbycategory widget_recent_entries">';
11 while ( $the_query->have_posts() ) {
12 $the_query->the_post();
13 if ( has_post_thumbnail() ) {
14 $string .= '<li>';
15 $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
16 } else {
17 // if no featured image is found
18 $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
19 }
20 }
21 } else {
22 // no posts found
23 }
24 $string .= '</ul>';
25
26 return $string;
27
28 /* Restore original Post Data */
29 wp_reset_postdata();
30 }
31 // Add a shortcode
32 add_shortcode('categoryposts', 'wpb_postsbycategory');
In particular, keep in mind that you should change “news” to your own category slug (category nice name used in URLs). and then save your work. Not only that, you can also use a comma to separate numerous categories.
In reality, this code just instructs WordPress to show 5 recent entries from the “news” category. The posts are then shown in a bulleted list with thumbnail-sized featured photos. Finally, it will generate the [categoryposts]
shortcode. As a result, you are able to paste into a page, post, or sidebar widget.
How to use shortcodes to display posts from category WordPress?
With this proven way, you just simply need to update the post or page where you wish the recent postings to be displayed by category. After clicking on the add new block (+) button, you need to add a shortcode block to your content area.
Then, in the block settings, you have to enter your shortcode [categoryposts]
.
Now, you are able to preview or save your post or page in order to check how your recent posts list looks.
However, if you are not satisfied with the presentation style, you are allowed to make changes by adding some custom CSS code to your WordPress theme.
Furthermore, to utilize the theme customizer, you must navigate to Appearance > Customize and then to the “Additional CSS” tab in the left column.
As a starting point, you are able to take advantage of the custom CSS code below.
1 ul.postsbycategory {
2 list-style: none;
3 }
4 .postsbycategory li {
5 padding-bottom:5px;
6 }
7 .postsbycategory img {
8 margin-right:5px
9 }
In particular, keep in mind that to save your custom CSS code by clicking the “Publish” button. At this time, you can browse your recent postings in a more organized manner by visiting your post or page.
How to add recent posts by category shortcode to WordPress sidebar?
Best of all, the same shortcode can be used in your WordPress sidebar or other widget-ready section.
To do so, you just need to add a “Text” widget to your sidebar by navigating to Appearance > Widgets.
Then, you may paste your [categoryposts]
shortcode into the widget’s text area.
In particular, remember to save your widget settings by clicking the Save button.
After that, you may see your recent articles by category widget in action on your website.
In conclusion
To sum up, this article lets you know how to display posts from category WordPress through different ways. You can apply any method depending on your strength and knowledge.
Overall, Arrowtheme hopes our article effectively instructs you how to use WordPress to display recent posts from a certain category. For any questions as well as problems, don’t hesitate to contact us right away!
Read More: Why WordPress password protected page not working?