Display recent posts of a category in WordPress

  1. Previous
  2. Next
  3. Recent

    1. Signs 'He' May be Cheating on You
    2. 7 Vows of Hindu Marriage - Know What 'Saat Vachans' Mean
    3. RuPay vs Visa vs Mastercard vs American Express- A Comparison
    4. What is RuPay? Everything You Need to Know About India's Domestic Payment System!
    5. Top 10 Online Portals to Find a High Paying Job in India
    6. Top Indians who Revolutionized the IT Sector in India
    7. 8 Initiatives by Narendra Modi that Could Change the Future of the Nation
    8. 8 Work-from-home Jobs for Housewives, Students, and Part timers
    9. Complete Company Registration Process in India - Explained
    10. Change Name in India in 5 Steps - Filing Affidavit, Newspaper Ad & Gazette Notification

While reading article on a WordPress site you often see links to similar articles at the bottom or on the right side of the page. Usually, this is done using plugins. But plugins can slow down the website loading time.

In the blog, we’ll tell you an alternate and relatively easier way to do the same without using plugins, in just a few lines of code.

display recent posts from a particular category in wordpress blog

The PHP Code

// Get categories of current post [could be more than 1]
$categories = get_the_category();
// Initializing pointer that counts the number of posts displayed
$postsdisplayed = 0;

	// If categories are available
	if($categories)
	// Get each category by running loop
	foreach($categories as $category)
	{
		// Getting category id		
		$cid = $category->term_id;
		$args=array( 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'post_date', 'order' => 'DESC', 'numberposts' => 6 , 'category' => $cid );
	
		// Getting recent posts of category with it $cid
		$recent_posts = wp_get_recent_posts($args);

		// Get each post one by one
		foreach( $recent_posts as $recent ) 
		{
			// Getting images of post
			$images = get_children( array( 'post_parent' => $recent['ID'], 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );

			// Getting images exists
			if ( $images ) :
			$image = array_shift( $images );

			// Showing post name with thumbnail image
			echo "<a href=".get_permalink($recent["ID"])." title=Read ".$recent["post_title"]." >";
			echo "<p class='posts'>";
			echo $image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );
			echo "<br>".$recent["post_title"]."</p></a>"; // Showing post heading
			++$postsdisplayed;

			// Show 6 posts only
			if($postsdisplayed > 5){break;}
			endif;
		}

	}

[Note: To change number of posts to display, change the value from 5 to any other in LOC 36]

The Idea

In WordPress, posts are divided by categories. To get recent posts of a category, all we have to do is to get the category id of current article calling the get_the_category() function and then fetch the recent 5, 10 or 15 posts of that category calling wp_get_recent_posts( $args ) function.

We get all images of a post calling get_children() function and then retrieve the thumbnail image using wp_get_attachment_image().

That’s it!. With little CSS styling, you can make these recent posts appear just like the product on any plugin.

Why avoid plugins?

Plugins are heavy. They load a number of JavaScript, CSS and image files that make websites slower. Slower websites are not likable to readers as well as search engines, and consumes extra bandwidth. So its always a good idea to use simple coding rather than relying on bulky plugins until required.

Moreover, more customize in appearance is possible if you have code in hand. Very few WordPress plugins provide good customization options.

blog comments powered by Disqus