How to Display Posts From a Specific Category in WordPress
Few things break the flow of browsing through a website quite like having to scroll through a long list of unrelated posts. Suddenly, rather than clicking through to the next page, your user is hitting the close button and heading to a different site.
The solution is simple: narrow the focus of displayed posts to a particular WordPress category. Now, a potentially annoying UI element that breaks focus becomes a powerful tool for encouraging users to explore deeper into your site.
There are a few ways to list posts by category in WordPress. You can use a code snippet, or you can employ a WordPress plugin to simplify the process. In this article, we’ll walk you through exactly how to implement both methods. Let’s get started!
Why Display Posts by Category?
There are a number of reasons you might want to display posts from a particular category:
- You can tailor the suggested posts to be more relevant to a given page. This makes it more likely that readers will click through to additional content.
- You can create a special WordPress category for posts you want to highlight and display them across your site, drawing attention and traffic their way. This is especially useful for featuring important posts from a category on the front page of your WordPress site.
- You can improve the overall flow of your site. The default ‘recent posts’ widget that comes with WordPress simply displays the latest posts, regardless of category or relevance. This is not ideal from a UX standpoint. If the listed content isn’t relevant, it becomes an obstacle the user has to get around, rather than something that enhances the experience.
These are just a few of the ways you can use this technique, but they illustrate how valuable it can be. With that in mind, let’s look at how to get your posts listed by category, starting with the manual approach.
List Posts by Category by Using a Code Snippet
The first method we’ll cover is using a code snippet that instructs WordPress to show all posts in a specific category. The main reason to use a snippet over a WordPress plugin is to get maximum flexibility. If you’re comfortable with code, you can incorporate just about any features you want into your WordPress post list. You can also style it however you like.
For our example, we’ll create a shortcode that displays the last five posts from your chosen category. You can insert this shortcode into any widget area using a text widget, or add it to a WordPress post or page using the shortcode widget in the Block Editor.
To create the shortcode, first navigate to your theme’s functions.php file. You can find this by going to your WordPress dashboard and navigating to Appearance > Theme Editor. The functions.php file will likely be titled Theme Functions in the list to the right:
Next, paste this code snippet at the bottom of the file:
function Last5posts() {
$args = array( 'posts_per_page' => 5, 'category_name' => 'uncategorized');
$last_5_posts_query = new WP_Query( $args );
while($last_5_posts_query->have_posts()) :
$last_5_posts_query->the_post();
$link = get_permalink();
$title = get_the_title();
$date = get_the_date();
$content .= '<div class="latest-posts">';
$content .= '<h3><a href='.$link.' target="_top">'.$title.' / '.$date. '</a></h3>';
$content .= '<p class="excerpt">' .get_the_excerpt(). '</p>';
$content .= '</div>';
endwhile;
return $content;
}
add_shortcode('Last5Posts', 'Last5posts' );
Make sure to replace the category name in the third line with your desired category. This snippet will create the shortcode [Last5Posts], which will display the most recent five posts from the selected category.
Once that’s done, simply click Update at the bottom of the page and you’re all set. Now you can insert this shortcode into a text widget or shortcode widget, to display the latest relevant posts:
To create multiple shortcodes for different categories, just paste the snippet into your functions.php file again. All you need to do is modify the shortcode details.
List Posts by Category by Using a Widget Plugin
If the mention of code snippets makes you uncomfortable, or you prefer a simpler solution that doesn’t require altering your site’s files, don’t worry. There are a number of excellent WordPress plugins that list posts by category. Many of them also offer robust configuration and customization options, so you’re not giving up much by going this route.
If you’re not sure where to start, you can try out the Recent Posts Widget Extended plugin. This tool adds a block to your editor that you can insert anywhere to display posts from any category (or all of them). It’s a little older, however, so you’ll want to test it out carefully before using it on your live site.
The plugin gives you a widget for use in sidebars and footers, as well as a block that can be embedded in any page or blog post. The block offers plenty of options: you can adjust the layout, number of posts, whether to show featured image thumbnails, and more:
The widget has a similar set of features, and can be accessed from the Widgets section in your WordPress dashboard:
Simply customize the parameters however you’d like, and select the Update button at the top of the page to save your changes.
If you aren’t a fan of this particular solution, you can easily find more options. There are plenty of plugins that let you list posts by category, so feel free to experiment with multiple tools to find the one that works best for you.
Keep Your Site Content Optimized with WP Engine
Displaying a list of posts from a specific category is a smart way to help move visitors further into your WordPress site, and increase traffic to other pages. It also makes for a better user experience all-around.
To create a truly fantastic website, however, you’ll need a strong foundation. Here at WP Engine, we offer extensive resources to help users and developers create an incredible digital experience for their customers (not to mention world-class WordPress hosting)!