I want to use a fallback image if no featured image is set. I'm using the following code, but the image is not shown...

<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
    <?php the_post_thumbnail( array(334, 259) ); // Declare pixel size you need inside the array ?>
<?php else : // No thumbnail? Showing default is better UX than no image. ?>
    <img src="/wp/wp-content/themes/klicknet-theme/images/testbild.png" 
alt="testbild" width="334" height="259" title="Bild: <?php the_title(); ?>"> 
<?php endif; ?>

Any ideas why?

  • It's probably something simple like the wrong URL to the image. First set your else statement to something basic like echo 'No featured image found'; so you can tell whether "else" is ever being met. If it is, then the URL to your image is wrong - unless you have your whole site installed in a "wp" folder, you probably just need to remove /wp from the very beginning of the img src.
    – WebElaine
    Jul 3, 2018 at 15:17
  • no i checked it...
    – tom84
    Jul 4, 2018 at 14:26
  • Where are you running this code? Are you sure you're in the Loop so has_post_thumbnail() knows which Post you're asking about?
    – WebElaine
    Jul 5, 2018 at 13:51
  • Regarding debugging the image url - first, go to your.domain + the path in src, to check if browser will load the image in the first place. Otherwise I recommend using either get_template_directory_uri() or get_stylesheet_directory_uri() then you don't have to worry where your themefolder is :) developer.wordpress.org/reference/functions/… , developer.wordpress.org/reference/functions/… e.g. src="<?php echo get_template_directory_uri(); ?>/images/testbild.png" in your case
    – jave.web
    Feb 9, 2021 at 2:05

2 Answers 2


Your code looks OK and it should work just fine. But there are some things you can (and you should) fix.

1. You don't use absolute URL for your fallback image

You pass /wp/wp-content/themes/klicknet-theme/images/testbild.png as src of your image. It would be much better and more secure if you'd use WP functions in there. For example like so:

<img src="<?php bloginfo('template_url'); ?>/images/testbild.png">

2. You don't escape the title properly

In your fallback image you use the_title() in title attribute. But you don't escape it as an attribute. If the title contains " character, it will break your HTML. Another problem is that the title can contain HTML tags, and they will be printed in your attribute.

If you want to use title as attribute, you should use the_title_attribute function instead. So the fixed version of that line can look something like this:

<img src="<?php bloginfo('template_url'); ?>/images/testbild.png" alt="testbild" width="334" height="259" title="<?php the_title_attribute( array( 'before' => 'Bild: ', 'after' => '' ) ); ?>"> 
  • 1
    I'd suggest using get_theme_file_uri() over bloginfo() these days. Better child theme support that way. Jul 4, 2018 at 5:53
  • thanks for your answer... but the default image is not loaded <div class="image-wrapper"> </div> the div is empty and only shows "" this....
    – tom84
    Jul 4, 2018 at 14:19

You could add a filter to provide custom <img> html when there's a thumbnail missing.

Note, that although you don't have to anymore this still allows you to use has_post_thumbnail() if necessary, because has_post_thumbnail() is based on get_post_thumbnail_id(), meaning it checks if there really was any image set to that post.


// fallback thumbnail
// note, that this still allows you to check has_post_thumbnail() !
// (since it correctly checks if ID is set, not the actual output :))
add_filter('post_thumbnail_html', function ($html) {
  if (! empty($html)) return $html; // FILTER ! MUST RETURN!
  return '<img src="' . get_stylesheet_directory_uri() . '/images/fallback.png' . '">';
}, 999, 1);

Just to be clear, this code assumes you have an images directory in your theme with a fallback.png insside it :) !
If you are expecting child themes, but the fallback image should come from your parent theme, then replace get_stylesheet_directory_uri() with get_template_directory_uri()

Any template code:

<?php the_post_thumbnail( ... ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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