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

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

August 31, 2016

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.

Prev Post

An Awful Blogging Journey

August 30, 2016

Next Post

Mark Zuckerberg - The Big Question...

September 1, 2016

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from us

You have Successfully Subscribed!