Set the first image of WordPress post content as featured

By Faruque Ahamed Mollick

Generally, all WordPress themes support feature image or the thumbnail image. From the WordPress post editor page of WordPress admin panel, you can set the featured image. You can choose an image from the bottom right side of the post editor page and then you can upload or choose a featured image from WordPress media.

But what happened if you have not chosen a featured image during publishing a post on your WordPress blog? Well, there will be no featured image shown in these case.

Optimized Faster Speed WordPress Hosting From Ultra Web Hosting

5 Best Schema Markup And Rich Snippets WordPress Plugins

But here I am going to show you a tricks using which you will be able to display the first image of your post content as the featured image if no featured image is set. But to do this you have to write some code into your theme’s PHP file. Part of the code will be in your theme’s functions.php file and the rest of the code will be used to display the featured image.

At first, you need to be sure if your theme support featured image. Adding the below code will make a theme featured image supported:

add_theme_support( 'post-thumbnails' );

Put the above code in your theme’s functions.php file only when your theme does not support thumbnail or featured image.

But most probably, you don’t need to follow the above step as almost all the themes support featured image.

Now to get the first image from the WordPress post content put the below code inside the functions.php file of your theme:

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

if(empty($first_image)){ //Defines a default image
// Set the default image if there are no image available inside post content
$first_image = "/img/default.jpg";
}
return $first_image;
}
// End Get First Image

After that, you need to call the above function in WordPress template file where you want to show the first image of the WordPress post. There is a tricky way which will first check if featured image is available for a post or not and then if it is not available, it will return the first image from the WordPress post as featured.

Must Use Free WordPress Plugins Every WP Blog Should Have

Below is the code that you need to place inside the template file where you want the featured image to appear:

<?php
// This code must should be inside WordPress post loop
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
echo get_first_image();
}
?>

In the code, you can see that I have used the if else statement to check if the featured image exists or not and then if it does not exist, it returns the first image of WordPress post as the featured image.

So have you enjoyed this post? Now whenever you forget to place a featured image or you don’t want to waste your time for selecting and uploading a featured image then if there is an image inside your post available, it will automatically display that image in the place of the featured image. So no need to worry if you forget to set a featured picture.

I hope this post will be helpful for you.