Get Last Posts with Thumbnail Shortcode

 

It can be edited a little bit, to have the option to get the featured image and if empty, then to get the thumbnail.  You can pick what you like.


function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];

// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}

function my_recent_posts_shortcode($atts){
 $q = new WP_Query(
   array( 'orderby' => 'date', 'posts_per_page' => '20', 'category' => 'web-design,graphic-design' )
 );

$list = '<ul class="recent-posts">';

while($q->have_posts()) : $q->the_post();

 $list .= '<li><img src="'.catch_that_image().'" width="50" height="50"' . '<a href="' . get_permalink() . '">' . get_the_title() . '</a>' . '<br />' . get_the_excerpt() . '</li>';

endwhile;

wp_reset_query();

return $list . '</ul>';

}

add_shortcode('recent-posts', 'my_recent_posts_shortcode');

 

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article