Display Recent Posts by Category (Plugin Method)

 

 

 

 

 

 

 

 

 

First thing you need to do is install and activate the Category Posts Widget plugin.

Upon activation, you need to visit Appearance » Widgets, there you will notice the new Category Posts widget in the list of available widgets.

Simply drag and drop Category Posts widget to a sidebar where you want to display recent posts by category.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The widget options are quite self explanatory. First you need to provide a title for the category posts section and choose a category. After that you can choose other display options like number of posts, excerpts, featured image, etc.

Once you are done, click the save button to store your widget settings. You can now visit your site to see recent posts by category in action.

Display Recent Posts by Category without a Plugin (Code Snippet)

In this method, we will use a code snippet to display recent posts from a category.

First you need to add this code in your theme’s functions.php file or a site-specific plugin.

 
function wpb_postsbycategory() {

// the query

$the_query = new WP_Query( array( 'category_name' => 'announcements', 'posts_per_page' => 10 ) );

 

// The Loop

if ( $the_query->have_posts() ) {

    $string .= '<ul class="postsbycategory widget_recent_entries">';

    while ( $the_query->have_posts() ) {

        $the_query->the_post();

            if ( has_post_thumbnail() ) {

            $string .= '<li>';

            $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';

            } else {

            // if no featured image is found

            $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';

            }

            }

    } else {

    // no posts found

}

$string .= '</ul>';
return $string;
 
/* Restore original Post Data */

wp_reset_postdata();

}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');
// Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');

 

 

The end of code////////////

Make sure that you replace 'announcements' with your own category slug.

This code simply queries WordPress to retrieve 10 posts from a specified category. It then displays the posts in a bulleted list. If a post has a featured image (post thumbnail), then it will show the featured image as well.

In the end, we created a shortcode 'categoryposts' and enabled shortcode in text widgets.

There are three ways of displaying the recent posts by category using this code snippet.

First, you can simply paste the following code anywhere in your desired template file location (such as footer.php, single.php, etc).

<?php wpb_postsbycategory() ?>

Second and third method relies on using the shortcode in the widget area or inside your posts / pages.

Simply visit Appearance » Widgets and add a text widget to your sidebar. Next add[categoryposts] shortcode in the text widget and save it. You can now preview your website to see recent posts by category in the sidebar.

If you want to show recent posts by categories on specific post or pages, then simply paste the shortcode in the post content area.

By default, your list may not look very good. You will need to use CSS to style the category posts list. You can use the code below as an starting point in your theme or child theme’s stylesheet.

ul.postsbycategory {

list-style-type: none;

}

 

.postsbycategory img {

float:left;

padding:3px;

margin:3px;

border: 3px solid #EEE;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

That’s all, we hope this article helped you display recent posts by category in WordPress sidebar. You may also want to check out these most wanted category hacks and plugins for WordPress.

Content taken from wpbeginner.com