Recent Posts Widget with Thumbnail – Without Plugins

In this post, we will see about showing Recent Posts Widget with Thumbnails in Sidebar.

Copy-Paste the below code in sidebar.php after deciding the placement of your Recent Posts. I’ve added just before so that this recent posts gets displayed first in the sidebar followed by all the widgets that is chosen by us under Appearance’s Sidebar widget area.

<?php $recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
&nbsp;&nbsp;&nbsp; if($recent['post_status']=="publish"){
	if ( has_post_thumbnail($recent["ID"])) {
		echo '<li style="padding:5px;border-bottom:1px solid goldenrod;">
		<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   get_the_post_thumbnail($recent["ID"], 'thumbnail'). $recent["post_title"].'</a></li> ';
	}else{
		echo '<li style="padding:5px;border-bottom:1px solid goldenrod;">
		<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a></li> ';
	}
     }
}
?>

Note:

  • wp_get_recent_posts() method does not filter the post status. So, check is made to display only “Published” post.
  • wp_get_recent_posts() by default returns 10 posts. We can do many customizations according to our need.
  • Thumbnail sizes can be defined in the second argument of get_the_post_thumbnail().
  • CSS Styling can be customized based on the requirement.

Try adding this PHP code to Display Recent Posts with Thumbnails in your wordpress blog.

https://officetricks.com/display-recent-posts-with-thumbnails-without-plugins/
«
»
ACF (3)
Admin Column (4)
Breadcrumbs (2)
CSS (6)
Custom post types (4)
Custom Taxonomies (3)
Customizer (4)
Hooks & Filters (3)
Jquery (9)
LAMP (10)
Login (5)
Meta boxes (6)
Multisite (2)
My Code (1)
Open Graph Meta Tags (1)
Redirects (2)
SEO (4)
Theme Building (5)
Title Tags (2)
Typography (3)
Uncategorised (54)
User Roles & Capabilities (2)
White labeling (9)
Working with Images (11)
Working with videos (2)
WP Database (1)