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?
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?
Share Improve this question edited Jul 3, 2018 at 18:28 Krzysiek Dróżdż 25.5k9 gold badges53 silver badges74 bronze badges asked Jul 3, 2018 at 14:09 tom84tom84 2551 gold badge6 silver badges21 bronze badges 4 |2 Answers
Reset to default 1Your 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' => '' ) ); ?>">
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.
functions.php
// 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( ... ); ?>
else
statement to something basic likeecho '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 theimg src
. – WebElaine Commented Jul 3, 2018 at 15:17has_post_thumbnail()
knows which Post you're asking about? – WebElaine Commented Jul 5, 2018 at 13:51get_template_directory_uri()
orget_stylesheet_directory_uri()
then you don't have to worry where your themefolder is :) developer.wordpress/reference/functions/… , developer.wordpress/reference/functions/… e.g.src="<?php echo get_template_directory_uri(); ?>/images/testbild.png"
in your case – jave.web Commented Feb 9, 2021 at 2:05