How to display a default image for post thumbnail using functions.php?

Question

I want to display a default featured image if there hasn’t been one set. I googled for a solution and all I could find was this piece of code (below). My question is, is there a way of displaying a default image by writing a function inside the functions.php? I don’t want to repeat myself with the code.

<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
0
Mahnenwolf 4 months 0 Answers 10 views 0

Leave an answer