Family Encyclopedia >> Electronics

How to Display Recent Posts by Category in Your WordPress Sidebar

Want to showcase the latest posts from specific categories right in your WordPress sidebar? We've helped countless users add this feature effortlessly. In this expert guide, we'll walk you through two proven methods: a beginner-friendly plugin and a custom code snippet for advanced customization.

How to Display Recent Posts by Category in Your WordPress Sidebar

Video Walkthrough

Subscribe to WPBeginner for step-by-step video tutorials. Prefer reading? Let's dive in.

There are two reliable ways to display recent posts by category. The plugin method is ideal for beginners—no coding needed—and offers extensive options like thumbnails, excerpts, dates, and comments. The code method provides more flexibility without relying on plugins.

Display Recent Posts by Category Using a Plugin

Start by installing and activating the Category Posts Widget plugin.

Head to Appearance » Widgets. You'll see the new "Category Posts" widget. Drag it to your desired sidebar.

How to Display Recent Posts by Category in Your WordPress Sidebar

Configure it easily: set a title, select your category, choose post count, enable excerpts, featured images, dates, or comments. Click Save, then visit your site to see it live.

Display Recent Posts by Category Without a Plugin (Code Method)

For full control, add this code snippet to your theme's functions.php file or a site-specific plugin:

function wpb_postsbycategory() {
    $the_query = new WP_Query( array( 'category_name' => 'advertisements', 'posts_per_page' => 10 ) );

    // The Loop
    if( $the_query->have_posts() ) {
        $string .= '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            if ( has_post_thumbnail() ) {
                $string .= '<li>';
                $string .= get_the_post_thumbnail($post->ID, array(50,50)) . get_the_title() . '</li>';
            } else {
                // if no featured image
                $string .= '<li>' . get_the_title() . '</li>';
            }
        }
        $string .= '</ul>';
    } else {
        // no posts found
    }
    return $string;
    /* Restore original Post Data */
    wp_reset_postdata();
}

// Add shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');

// Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');

Replace 'advertisements' with your category slug (found in Posts » Categories).

This queries 10 recent posts from the category, lists them with thumbnails if available, and creates a [categoryposts] shortcode usable in widgets or content.

Three ways to use it:

  1. In templates (e.g., footer.php): <?php echo do_shortcode('[categoryposts]'); ?>
  2. In a Text widget: Add [categoryposts] via Appearance » Widgets.
  3. In posts/pages: Insert the shortcode directly.

Style the list with this CSS in your theme's stylesheet:

ul.postsbycategory {
    list-style-type: none;
}
.postsbycategory img {
    float: left;
    padding: 3px;
    margin: 3px;
    border: 3px solid #EEE;
}

How to Display Recent Posts by Category in Your WordPress Sidebar

That's it! Now your sidebar highlights fresh category content. For more, explore our top WordPress category plugins and hacks.

Liked this? Subscribe to our WordPress YouTube Channel. Follow us on Twitter.