0

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' ); ?>
      </a>
    </div>
  • Do you want to do this by modifying templates or just with filters? – Krzysiek Dróżdż Apr 24 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? – Benjamin Franklin Apr 24 at 9:35
1

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' ); ?>
  </a>
</div>
<?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! – Benjamin Franklin Apr 24 at 11:28
1

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.

https://wordpress.org/plugins/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="<?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.

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.