Can anyone help?
I’m trying to show a default placeholder image on my posts when the user hasn’t uploaded an image.
I have the below code BUT i need to rewrite it so that the link isn’t applied to the placeholder image – only the uploaded image.
Unfortunately my PHP isn’t great at all so would appreciate some help..
<div class="imageContainer"><a href="<?php the_permalink(); ?>">
<?php
// Must be inside a loop.
if ( has_post_thumbnail() ) {
the_post_thumbnail('size-2');
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/shared/placeholder.jpg" />';
}
?>
</a></div>
You can filter the thumbnail HTML before it is printed out. If it is empty â return the placeholder.
This should go into your themeâs
functions.php
:In your template just use
No conditionals or complicated PHP needed anymore. 🙂