Displaying WordPress Recent Posts of a Specified Category

Last modified on May 18th, 2018 by Vincy.

Generally, the WordPress recent posts are displayed in the descending order of the date of publishing. This recent list of posts can be of any category under which it is posted. If you want to list the recent posts by a specified category, then this WordPress post will be helpful for you to implement it in your WordPress site.

WordPress category page lists recent posts by category. In a previous tutorial, we have seen how to add category menu by using walker class to navigate to the WordPress category page. If you want to display recent posts by category on the homepage or in the sidebar tile, it is easy to implement by changing the theme files or by creating custom widgets.

Code to Display Recent Posts by Category

In this section, I have shown the code to be used for displaying recent posts by category. In this code, I request WP_Query() to fetch posts by category. I am sending the category id and the limit as the parameters to execute the WordPress query. This function will return the posts data as required. By iterating the posts array data, the recent post list can be displayed. Add this code wherever you want to display the recent post list.

<?php
$post_by_category = new WP_Query( 'cat=19&posts_per_page=15' );
if ($post_by_category->have_posts()) :
?>
<h2 class="heading">Recent Posts</h2>
<?php
    while ($post_by_category->have_posts()) :
        $post_by_category->the_post();
        ?>
<div class="entry">
    <div class="post-image">
        <?php echo_first_image ( get_the_ID (), true );?>
    </div>
    <div class="post-data">
        <h2>
            <a href="<?php the_permalink() ?>"> <?php the_title(); ?>
            </a>
        </h2>
        <div class="desc">
            <?php the_excerpt_rss(); ?>
        </div>
    </div>
</div>
<?php
endwhile;
endif; 
?>

Displaying Recent Posts by Category using WordPress Plugins

There are WordPress plugins for displaying recent posts by specifying one or more categories. The plugin configuration page will have controls to customize settings to display recent post. Generally I try to resist recommending WordPress plugins. But, I get frequent mails asking for suggestions. To display recent posts by category you may consider the plugins, Recent Posts by Category Widget, Recent Posts Widget Extended plugin.

This WordPress code tutorial was published on May 18, 2018.

↑ Back to Top

Share this Article