By default, wordpress has only one function for us to display an image as thumbnail or featured image – upload this image direct to your host.

In this article, I will help you with two different ways to use an external image as thumbnail.

1. Using Code.

With this ways, the ideas is we create a new function. This function automatically get the first image in an post / article. Then we just replace the the_post_thumbnail() with our new function.

Add the function below to your functions.php file

function get_first_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];

 if(empty($first_img)) {
   $first_img = "/path/to/default.png";
 }
 return $first_img;
}

Then, in the index.php file or the content.php file, find the the_post_thumbnail() and replace with

<img src="<?php echo get_first_image(); ?>" alt="<?php the_title(); ?>" />

Do it the same with archive.php, category.php, tag.php if needed.

2. Using Plugin

If you don’t familiar with using code, take a look with these plugin below, I know you love it

Share with your friends