Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Set a Default Fallback Image for WordPress Post Thumbnails

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Set a Default Fallback Image for WordPress Post Thumbnails

WordPress has this very cool post thumbnail feature which was added in version 2.9. This feature basically 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 post thumbnail (featured image) which causes the template display to break. Second, it just looks unprofessional to have broken images or empty areas on homepage where the thumbnail was suppose to be. We believe that theme designers and developers 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 a post thumbnail (featured image), then the first image attachment within the post will be displayed as the thumbnail. In this article, we will show you two different ways of adding a default fallback image for WordPress Post Thumbnails.

To utilize the tricks in this post, you have to enable the post thumbnails in your WordPress theme.

Adding a Branded Image as the Default Fallback

Often when creating a custom site for a client, they may have posts that has absolutely no image. So it is always smart for designers to create a branded image that is displayed for posts that has no thumbnail specified. The philosophy of this idea is similar to having a customized gravatar in WordPress. 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, single.php, loop.php, index.php, archive.php, etc). Then simply paste the following code within the post loop.

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

Explanation: The code checks if the post has a specified thumbnail, and if there is, then it will display the post thumbnail. If there is no post thumbnail specified, then it will display ‘default-image.jpg’ image from your theme’s images folder.

This is the very basic example. You can obviously expand on it with thumbnail sizes, class and much more.

Adding the First Post Image as the Default Fallback

Often new bloggers add images to the post, but either they forget to specify a post thumbnail, or they simply do not know how to. Regardless of what the case is, we as developers should prepare in advance. So the trick that we have seen used specially in theme frameworks like Genesis is adding the first post image as the default fallback for WordPress post thumbnails. To do this, you have to 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
  if($files) :
    $keys = array_reverse(array_keys($files));
    $num = $keys[$j];
    $image=wp_get_attachment_image($num, 'large', true);
    $imagepieces = explode('"', $image);
    $imagepath = $imagepieces[1];
    print "<img src='$main' alt='$the_title' class='frame' />";

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();
} ?>

Explanation: First we create a function that gets the first image attachment for each post. In our theme file code, we check if the post has a thumbnail. If the thumbnail exist, then we display the thumbnail, and if it does not exist, then we display the fallback.

Source: Snipplr

This is just the start. You can get creative with the fallback images for WordPress post thumbnails. You can combine the two techniques mentioned above, create branded images for each category, and much more.

If you are a theme developer, then make sure you have this in your themes.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. JAVAD says:

    hi i use auto-post-thumbnail to create auto thumbnail

    now i want if i enable show excerpt in my theme , first image or featured image shown on above the post in index.php

  2. Siddharth says:

    Great! It is easy to understand now. I have seen different preference of feature images in many different themes but your blog post made sense.

  3. Laura P. says:

    How can I tweak this function for adding a specific image (example.jpg) as the default image for post type: topic? Basically, I’m trying to set a default image for bbpress topic pages.



  4. Jose Salazar says:


    Im adding the First Post Image as the Default Fallback, but would like to display attachment image ONLY if size in pixels is between 460×350 and 700×525. Is that possible? Any suggestions?

    Thanks in advance!

  5. Paal Joachim Romdahl says:


    Could someone update the above code but this time to be added into the functions.php file?

    Perhaps even add on to how to define various category post images.

    Thank you!

  6. Nestor Cheese says:

    Works great, thanks! The thing, though. is that the default thumbnail links automatically to the post but if you add a featured image, it doesn’t link. Has anyone resolved this?

  7. Chandra says:

    Is this possible to display as slider from the single post uploaded images…

  8. wfriley says:

    I pasted the code into the functions.php file and checked my site to make sure nothing went haywire. For some reason my posts are now ending up on my static home page. I removed the code but it did not correct the problem. Any chance you might know a fix for that? I tried recreating the home page but the same thing happens.

  9. Alexandre Michel says:

    Hi, looks like it’s working but in my single post page the default image does not show up. It returns blank code. Would you happen to know why?

  10. Mitch Larouche says:

    How can I do an if statement where if there is no FEATURE or no MEDIA image then show google ad? This works but I want to add and IF for when I add a media image also. So if there is NO media image or feature image INCLUDE the adsense…php ad.

    ?php if( has_post_thumbnail() ) { ?>
    ?php } else { ?>
    ?php include(‘adsense_singlepost_top_square.php’) ?>
    ?php }

    Can’t see to figure out how to include the media file from the post?

  11. Dennis says:

    How can i add a default image for woocommerce products?

  12. Chetan Patel says:

    Hello I have Wallpapers site on wordpress. I have little problem with images. Google Indexing my thumbnail instead of full size images. I want to index only full size image. Any way to do this. ?? please help ,me.

  13. Doop says:

    This is great except if you upload an image to a post and then decide to delete it, it will still be attached to the post.

  14. darkpollo says:

    Will this work with external images too? Or only with the ones hosted on the same domain?

    Thank you

  15. Byron says:

    I’m tying to set a default image for one of my custom post types. I’ve not been able to do this and have tried many of the plugin on the WP repository… Default featured image sets a featured image to All post types even Ubermenu.

    I need to limit this to one post type. Do you have any suggestions for me please?

    • WPBeginner Support says:

      Byron, does the custom post type you want to set the default thumbnail for has featured image support? If yes then using this code in your theme template should display the default post thumbnail.

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

    Okay, that didn’t work…! The a href tag messed it up. How do I show you code?

    <a href="" title="" >

    Try that!

  17. Alejandro says:

    Excellent solution. Now, how I can use this to assign to a specific category and taking ramdom images from a folder?

    <img src="/images/default-image.jpg” alt=”” />

    Thank you a lot!

    • WPBeginner Support says:

      This requires a more complicated solution. First you would want to set an image for each category and upload them to a specific folder probably using category slug as the image file names. Then you need to get the category slug for each post and use it as the fallback image URL.

  18. Konstantin says:

    I am having a small problem, when calling the image, it only shows the full image, not the ones I try to specify (small, medium, thumbnail, etc).

    Did anything change in wp3.7+? The images are there, and are being created, but they wont display :(

  19. Thiago says:

    Thank you!!

  20. Arthur Okonkwo says:

    please is there a plugin that can do this, cos i don’t want to mess with with my code.

  21. Susan Clifton says:

    Will this work with a custom post type? I’m building a Portfolio theme. Your tutorials are always so helpful. I don’t really know PHP but I’m comfortable with messing with it.

  22. Tania says:

    Is there a way to show a default “image not available” image when the source image src is empty ?
    I’m promoting amazon products in my blog (using API) but most of the products do not have image. By providing a default “image not available” image, I can help my visitors to understand that the sellers do not provide any image.

    I prefer html, css or javascript solution.

    thanks and sorry about my english ;)

    • Editorial Staff says:

      The second option should work for you then. Just upload the image that says image not available.

  23. Waheed Akhtar says:

    Hi Balkhi,

    I have some old posts where post thumbnail is not defined. Also may be for some posts there’s no image uploaded (did it manually through FTP and linked in the post). The result is I don’t see any featured thumbnail for the post.

    Is there a way we can grab the first image, resize it (e.g. 200 x 200) and show as featured image?


  24. Amin says:

    Thanks for this post. I’ve been searching for days to find a way to have an automatic default featured image. This saved me so much time! Thanks a lot.

    • Amin says:

      You can also try the Default Thumbnail Plus plugin. Works very well, and no coding necessary!

  25. James says:

    I’ve tried to merge both options but failed.

    How would you Check for Thumbnail, but if none then check for First Post Image, but then if none then post Default Branded Image?

    • Rory says:

      Hi, have you found a way to do this? I’m still on the hunt for an answer, thanks.

      • Editorial Staff says:

        Try this:

        function get_fbimage() {
          if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) {
          $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '', '' );
          $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 = //Define Default URL HEre;
          return $fbimage;
        • Vanessa K says:

          Hi :), great tutorial. I’m a somewhat beginner for a lot of wordpress code. I tried to follow this tutorial for setting up the fall back image a few months ago and failed :p but was able to successfully implement it just now. Yay me! haha. However I would really like to set it up in the way James mentioned…

          “How would you Check for Thumbnail, but if none then check for First Post Image, but then if none then post Default Branded Image?”

          Posting the code you offered in response to his request is only showing up with syntax errors for me in my functions file :/. I’m sure it’s me and not the code :p but any suggestions? Thanks! And Happy Thanksgiving!

  26. jfwebdesignstud says:

    wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), ‘full’ );
    can this be used somehow with this for including pinterest featured image

    • wpbeginner says:

       @jfwebdesignstud You can most certainly specify your thumbnail as the image for Pinterest.

  27. CharlesUibel says:

    Yes But I don’t want to just display the fallback image, I want to assign the image to permanently be the featured image.

  28. masoudgolchin says:

    great tutorial , thanks !

  29. DirTek says:

    Great tutorial, mate. Been useful for my “related posts” section. Cheers!

  30. leitseitenmacher says:

    how do you create a real fall back thumbnail?

    with the solution a post rhumbnail is shown but therefore not registerate in the backend.

    any solution

    • wpbeginner says:

      @leitseitenmacher The whole idea of a fallback is to show when nothing is selected at the thumbnails area in the backend… so NO you will not be able to see it in the backend….

      • leitseitenmacher says:


        well i allready noticed that.

        but the question was: is it possible in wordpress to register a standard post thumbnail via php code so you can see it in the backend?

        • leitseitenmacher says:


          don’t believe the hype.

          it#s possible as always check out this remarkable plugin.

        • wpbeginner says:

          @leitseitenmacher Great find. Again, there is no HYPE that we are trying to create. It is impossible to keep track of what exists in the plugin sphere of WordPress because there are thousands.Just tested out that plugin… It doesn’t register those thumbnails in the database of each post. It is just hooking and giving you a visual display of what you selected in the plugins setting.

          If that is what you want, then sure.

          The article above accomplishes exactly the same thing without going the extra lengths of visually displaying. In most sites the fallback is usually the site logo. So you don’t really need to see it. But we will do a writeup on the plugin you found. Thanks for the suggestion :)

  31. gal1982 says:

    Excellent solutions, i was looking for that for a while

    Is there a way to change the resize the image?

    I have tried for example:

    $image=wp_get_attachment_image($num, ‘medium’);

    But it doesn’t effects the image size. any idea?

  32. milohuang says:

    Nice tutorial. Have test the first method but it did not seem to work. Check the HTML and the default thumb does not exist (no img tag was found). However, if I use the Default Post Thumbnail plugin, it works. Any help? Running 3.2 locally. Thank you!

  33. sebastianjthomas says:

    Great! But how do you set the main_image size without CSS (which stretches the image)

  34. wp_smith says:


    I have written a plugin, <a href=””>Genesis Featured Images</a>, that will do this for the <a href=””>Genesis Framework</a>. I’d love to hear your thoughts!?

  35. sleepingsun says:

    Hello !

    Thanks a lot for this useful tip. How would I make the same function to work in RSS feeds please ? If a post thumbnail has been set : use it, otherwise : use the first image attached to the post.

    Any help would be much appreciated !

  36. Ayman Aboulnasr says:

    Thank you very much for the tutorial. I was actually thinking about this trick for a few days now.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.