Hello how can I do with this function if there is no post thumbnail loaded, show a default image
echo get_the_post_thumbnail( $page->ID, 'image' );
Hello how can I do with this function if there is no post thumbnail loaded, show a default image
echo get_the_post_thumbnail( $page->ID, 'image' );
Share
Improve this question
edited Jul 18, 2020 at 5:42
fuxia♦
107k38 gold badges255 silver badges459 bronze badges
asked Jul 18, 2020 at 4:47
StymarkStymark
372 bronze badges
1
- 1 Does this answer your question? Fallback default image when there is no featured image – Michael Commented Jul 18, 2020 at 15:40
1 Answer
Reset to default 2Create "images" folder in current active theme the put "default-image.jpg" in that folder.
<?php
if ( has_post_thumbnail( ) ) {
echo get_the_post_thumbnail( $page->ID, 'image' );
} else { ?>
<img src="<?php echo get_template_directory_uri().'/images/default-image.jpg'; ?>"/>
<?php } ?>
Use path according to the theme (parent or child).
get_stylesheet_directory_uri()
: url path to current Theme directoryget_template_directory_uri()
: url path to parent Theme directory