How to Display Recent Posts by Category in WordPress

In this short tutorial we will show you how to display recent posts by category in WordPress. The first method uses a plugin and is fairly simple and beginner friendly, it displays recent posts by category in a widget in one of your widget ready areas of your wordpress theme.

The second method uses a code snippet and is meant for our advanced readers. One of the advantages to using the code method is you will have more customization options to available to you.

The Code Method

Add the following code into the template you want to display your categories posts.

[php]

<?php
$cat_args = array(
‘orderby’ => ‘name’,
‘order’ => ‘ASC’,
‘child_of’ => 0
);

$categories = get_categories($cat_args);

foreach($categories as $category) {
echo ‘<ul>’;
echo ‘<h3><a href="’ . get_category_link( $category->term_id ) . ‘" title="’ . sprintf( __( "View all posts in %s" ), $category->name ) . ‘" ‘ . ‘>’ . $category->name.'</a></h3>’;

$post_args = array(
‘numberposts’ => 5,
‘category’ => $category->term_id
);

$posts = get_posts($post_args);

foreach($posts as $post) {
?>
<li>
</h1> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><h1>
<figure>
<?php the_post_thumbnail(); ?>
</figure>
<p>
<?php the_excerpt(); ?>
</li>

<?php
}
echo ‘<span class="view-all"> <a href="’ . get_category_link( $category->term_id ) . ‘" title="’ . sprintf( __( "View all posts in %s" ), $category->name ) . ‘" ‘ . ‘>View all posts in ‘ . $category->name.'</a></span>’;
echo ‘</ul>’;
}
?>

[/php]

The Plugin Method

Visit Category Posts Widget plugin page. Download and activate the plugin then visit your widgets page. You will notice the new Category Posts widget in the list of available widgets. Simply drag and drop Category Posts widget to any sidebar where you want to display your site’s recent posts by category.

image

Provide a title for the widget and choose which category your posts will come from. After that you can choose other display options like number of posts to be displayed, excerpts, post thumbnail, show / hide comment count, display post date, etc.