Skip to main content

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



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. 

Comments

Popular posts from this blog

Earn While you Save With Alat.ng

If you've always wanted to save up for the future, but didn't take actions for reasons best known to you, now is the time to reconsider your thoughts and take actions, because there is a digital bank known as ALAT waiting to be utilized. What Is A Digital Bank? According to Avoka.com; "Digital banking is the digitization (or moving online) of all the traditional banking activities and programs that historically were only available to customers when physically inside of a bank branch." What is ALAT? ALAT is a digital bank, the first if its kind in Nigeria! It is an innovation of Wema Bank Nigeria, developed to give you easy access to saving your funds and doing other bank transactions without being physically present at the bank.  With Alat digital bank, you can open a savings account using your phone number and Bank Verification Number (BVN) as well as other documents that you will be prompted to upload during the registration process. It’s simple, convenient, reliabl...

How To Add Instagram Feed To Your Blog

You probably have an Instagram account and would want to showcase your pictures in your blog. Adding an Instagram widget to your blog goes beyond just showcasing your account to gaining followers, who wouldn't want more followers? This post will be your guide in embedding an Instagram feed into your blog. For people do not use or have an idea of Instagram, to let you know about the beautiful platform, I will run through it briefly.  Instagram is an online photo and video sharing network which let you share photos and videos straight from your mobile device to your account for your followers viewing pleasure. With Instagram, you can automatically share your photos and videos on social media networks such as Facebook, Twitter, Tumblr, and Flickr. I will be sharing a couple of tutorials with you today that not require a plugin, and can be used on either WordPress or Blogger platform, although I will center more on blogger users. There are many websites that could work ...

An Unbiased Infolinks Review

Today, I will be going over the adnetwork, infolinks.com in my own way. let's find out if it is a good adsense alternative or one that can be used with adsense. Discovering Infolinks I found out about infolinks while searching for AdSense alternatives when I was unable to get AdSense approved for my account, Then I applied with my previous blog but darn, the blog probably did not meet their demands and I was never approved. Few months laters I got AdSense approved for my blog but that didn't live long enough, to my detriment and after been blown off by the infamous Adsense Disablement ish (made me ditch my previous blog)... Months after setting up my new blog, I had to look for an AdSense alternative, and every research I made, didn't exclude Infolinks, and this made me think highly of them and so I had to give an application another shot, this time within a day, I got approved to display ads on my blog... Bam! Such a nice achievement... Getting To Understa...