How Left-arm Seamer Thangarasu Natarajan Went From Tennis Ball Cricket To Landing Rs 3 Crore IPL Deal In Just 5 Years
February 21, 2017
How do I modify a post template in Wordpress
How do I modify a post template in WordPress?
February 21, 2017

How do I automatically set the first image found in my post from an external server as my featured image for my WordPress blog post?

How do I automatically set the first image found in my post from an external server as my featured image for my WordPress blog post?

How do I automatically set the first image found in my post from an external server as my featured image for my WordPress blog post?

WordPress has this very cool post thumbnail feature which was added in version 2.9. This wordpress feature allows you to add a custom image as a thumbnail that represents that post. This feature has become a standard for theme designers. But often, new bloggers forget to set a featured image (post thumbnail) which causes the template display to break.

Second, it just looks ignorant to have broken images or empty areas on the homepage where the thumbnail was supposed to be. We believe that theme developers and designers should create a new standard of adding a default fallback image for WordPress post thumbnails in their themes.

Popular frameworks like Genesis already has this type of option built-in where if the user does not specify afeatured image ( post thumbnail), then the first image attachment within the post will be displayed as the thumbnail. In this article, I will show you two different ways of adding a default fallback image for WordPress Post Thumbnails.

Adding a Branded Image as the Default Fallback

All you really have to do is open the theme file where you are going to display the post thumbnail, this can be (home.php, loop.php, single.php, index.php, archive.php, etc). Then simply paste the following code within the post loop.

<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>" />
<?php } ?>

Adding the First Post Image as the Default Fallback

open your theme’s functions.php file and add the following codes:

//function to call first uploaded image in functions file
function main_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment
&post_mime_type=image&order=desc');
  if($files) :
    $keys = array_reverse(array_keys($files));
    $j=0;
    $num = $keys[$j];
    $image=wp_get_attachment_image($num, 'large', true);
    $imagepieces = explode('"', $image);
    $imagepath = $imagepieces[1];
    $main=wp_get_attachment_url($num);
		$template=get_template_directory();
		$the_title=get_the_title();
    print "<img src='$main' alt='$the_title' class='frame' />";
  endif;
}

Then open the theme file where you are displaying the WordPress post thumbnail, this can be (home.php, single.php, loop.php, index.php, archive.php, etc) and paste the following code:

<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) {
  echo get_the_post_thumbnail($post->ID);
} else {
   echo main_image();
} ?>

In this way automatically set the first image found in my post from an external server as my featured image for my WordPress blog post.

Leave a Reply

Join Us

For Wordpress Tips || Wordpress Security || Blogging Tips || Website Development Guide.

100% Privacy. We don't spam.