Fallback default image when there is no featured image

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="https://wordpress.stackexchange.com/questions/336162/<?php the_permalink(); ?>">
        <?php the_post_thumbnail( 'post-card-cropped' ); ?>
      </a>
    </div>

3 Answers
3

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.

Default featured image

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() ) {
the_post_thumbnail();
} else { ?>
<img src="https://wordpress.stackexchange.com/questions/336162/<?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.

Leave a Comment