WordPress: Simple Image Substitue if no Featured Image is Selected

WordPress Post Thumbnails (also known as Featured Images) are a great feature that was added to WordPress in version 2.9. Though not “required” and often times not even utilized, there are certain instances where forgetting the post thumbnail will break a layout or the functionality of your page.

For example, I recently developed an eCommerce site where we were using the WP Featured Image as the default image for the shopping index page. There were often times when the client would be adding a large quantity of products at once, and would forget to select the post thumbnails. Or, another senario…let’s say that same client has a product they want to list, but there is no image available, for whatever reason they would like to list the product and come back to it later.


A developer friend of mine pointed out that you may want to include this solution in an external file (child theme etc.). I build my own themes so the thought never crossed my mind that these changes would be overridden if you ran an update…just something to note depending on your setup.

The Solution

WordPress Post Thumbnail FallbackLet’s setup a simple fallback image. This image can be whatever works in your case, in this case let’s just use an image with the text “Photo Coming Soon”.

Step 1.)

Open the file where the Post Thumbnail is currently being called, in this example it was index.php.

Step 2.)

Locate the current call to Post Thumbnail, it will look something like this:

<?php the_post_thumbnail(); ?>

Step 3.)

Replace the call to the Post Thumbnail with the following conditional:

<?php if ( has_post_thumbnail() ) {
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/img-coming-soon.jpg" alt="Image Coming Soon" />
<?php } ?>

Note: Replace “/images/img-coming-soon.jpg” with the correct path to your replacement image.

And that’s it! Pretty simple, really. There are of course many other ways of replacing or dynamically adding the Post Thumbnail, but this should give you a good place to start.


{ Respond }

Leave a response