Menu

How to create a default post thumbnail in WordPress

So you’re running a WordPress site and your articles use featured images. The issue is you don’t always have or want a unique image for every post you publish and in those instances need a fallback image to be displayed in its place automatically.

No problem! Here’s how.

First, create your default image. Save it as a .jpg file and upload it to your theme’s images directory.

Next, open your theme’s single.php template file (or include file, depending on how your theme is built).

Locate where your featured image is displayed in your post. In this example, I’ve wrapped it inside a figure element:

<figure class="featured-image">
  <?php the_post_thumbnail(); ?>
</figure>

Next, we replace the image element with following code:

<figure class="featured-image">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { ?>
  <img src="<?php bloginfo('template_directory'); ?>/images/your-default-thumbnail.jpg" alt="ADD THE DESCRIPTION OF YOUR DEFAULT IMAGE HERE" />
<?php } ?>
</figure>

The revised code checks for an existing featured image and if none was added when you published your post, the default image is shown in its place.

Save your changes and you’re good to go!

Note that you’ll also need to apply this code change to anywhere in your theme where you’re pulling in your post thumbnails. This includes snippets displayed on archive pages, your main index template, front page template, or footer. If you’re using include files to display the content, you may only need to make the change once or twice but go through each of your template files anyway just to be sure you don’t miss anything!

Adding thumbnail support in your WordPress theme

If you’re not already using post thumbnails, you’ll first need to make sure your theme supports them.

This is done by adding the following code to your theme’s functions.php file located in the root of your theme’s directory:

add_theme_support( 'post-thumbnails' );

This will add the Featured Image meta box located on the right of your post editor:

wordpress featured image meta box screenshot

Have fun!

Archived under:
Share:
This site uses cookies to collect basic info to help improve your experience. Read my Privacy Policy Accept