I have tried many options but without luck. It should be very simple approach but does not produce results. I am trying to insert default fallback image if there is no featured image in the post. My code that produces the thumbnail:

    <?php if ( has_post_thumbnail() ): ?>
    <div class="post-card__image">
      <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail( 'post-card-cropped' ); ?>
  • Do you want to do this by modifying templates or just with filters? Apr 24 '19 at 9:17
  • This a custom theme that will be released for re-use. I am assuming modifying templates would be smarter way to go? Apr 24 '19 at 9:35

Method 1: Set Default Fallback Image for Post Thumbnails Using Plugin

This method is easier and recommended for all users.

First thing you need to do is install and activate the Default Featured Image plugin.


Method 2: Add Fallback Image as Post Thumbnail Manually

Your theme’s images folder is located inside /wp-content/themes/yur-theme/ folder. If it doesn’t have the images folder, then you need to create it.

After you have uploaded the image to your website, the next step is to tell WordPress to look for this image when a post doesn’t have its own post thumbnail.

Your WordPress theme displays post thumbnails in various places. You need to look for the_post_thumbnail() function in theme files. Typically, you’ll find it in archive.php, single.php, or content templates.

Next, you need to add the following code where you want to display post thumbnail.

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

Don’t forget to replace default-image.jpg with your own image file name.


So you were almost there...

<?php if ( has_post_thumbnail() ) : ?>
<div class="post-card__image">
  <a href="<?php the_permalink(); ?>">
    <?php the_post_thumbnail( 'post-card-cropped' ); ?>
<?php else : ?>
    <!—-... here goes your fallback —->
    <?php echo wp_get_attachment_image( 10, 'post-card-cropped' ); ?>
<?php endif; ?>

In the example above I’m using attachment with ID 10 as default fallback image. But of course you can go further and create an customizable option for this. Or use some static html code/static image... the choice is yours.

  • Attachment ID is not perfect solution as we need this working by default. I have tried with static image with answer that Vasim provided, but for some reason none of these add default image. Can you provide you above working example with static image so I can test this one as I have so far tried 5 different code examples and none worked. Very strange. I am adding this in proper place and image path is correct. Thanks! Apr 24 '19 at 11:28

We can set a default image with the code below.

 * Default post thumbnail image.
 * @param  string $html The Output HTML of the post thumbnail
 * @param  int $post_id The post ID
 * @param  int $post_thumbnail_id The attachment id of the image
 * @param  string $size The size requested or default
 * @param  mixed string/array $attr Query string or array of attributes
 * @return string $html the Output HTML of the post thumbnail
function ns_post_thumbnail_fb( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
 if ( empty( $html ) ) {
    return sprintf(
        '<img src="%s" height="%s" width="%s" />',
        get_option( 'thumbnail_size_w' ),
        get_option( 'thumbnail_size_h' )

return $html;
add_filter( 'post_thumbnail_html', 'ns_post_thumbnail_fb', 20, 5 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.