Auto Use First Image In A Post As The Featured Image In Wordpress

Wednesday, August 31, 2016 No Comments

This post may contain referral links. For more info, please read my disclosure policy.

I don’t know if you have experienced this issue in your blog, although it really isn’t an issue, but for the lazy bloggers, oh well…

Today I’m going to teach you or rather give you a solution.

Some WordPress themes make use of featured image rather than the post’s first image and as a blogger,  you may not always have the time to set up a featured image for your post or sometimes, you just feel too lazy to do what you are supposed to do.

The code I’m about to give to you is a code I also use on my blog.

It is really easy to implement. All you have to do is go to your template editor, select functions.php and in the box, paste the code below after other codes.

 function get_src() {   if ( has_post_thumbnail() ) {  $src = wp_get_attachment_image_src( get_post_thumbnail_id(), ‘thumb’ );  $fbimage = $src[0];   } else {  global $post, $posts;  $fbimage = ”;  $output = preg_match_all(‘/<img. src=[\'”]([^\'”] )[\'”].*>/i’,  $post->post_content, $matches);  $fbimage = $matches [1] [0];   }   if(empty($fbimage)) {     $fbimage = site_url().’/wp-content/themes/epik/img/logo.png’;   }   return $fbimage; } add_filter(‘genesis_get_image’, ‘default_image_fallback’, 10, 2); function default_image_fallback($output, $args) {     return get_image(); } function get_image($class=””) {     $src = get_src();     ob_start()?>     <a href=”<?php echo get_permalink() ?>”>         <img class=”featured-image <?php echo $class ?>” src=”<?php echo $src ?>” alt=”<?php echo get_the_title() ?>” />     </a>     <?php return ob_get_clean(); }

Please be careful not to delete any code in your functions.php except you know what you are doing.

You only have to include the code above in your functions.php file. 

Note that this code is for genesis framework child theme. 

Blog Archive

Contact / Address

Mr Sunday Joseph's Compound,
Off Goodnews street, Azikoro,
Yenagoa Bayelsa State
Zipcode: 569101

Reach Us on

Blog Owned and managed By:
Joseph Onome.