Fallback featured image in WordPress

Do you want to set a fallback featured image?
If yes then here is the snippet for it. It is helpful when you do not have any featured image in your post, you can display your desired image or your company logo as your post featured image.

First, upload the image to your theme image folder. Your theme’s images folder is located in
/wp-content/themes/your-theme-name/ folder. If it doesn’t have the images folder, then you need to create it.
Now insert the following code where you want to display the image.

// Checking post have feature image or not

<?php if ( has_post_thumbnail() ) {

       the_post_thumbnail();

  } else { ?>

      // adding image if the condition is false 
     <img src="<?php bloginfo('template_directory'); ?>/images/feature-image.jpg" alt="<?php the_title(); ?>" />

   <?php } ?>

Don’t forget to change the image name instead of feature-image.jpg

Here we are checking if a post has a featured image or not if not then we are replacing it with the desired image.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top