admin管理员组

文章数量:1327975

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
Add a comment  | 

1 Answer 1

Reset to default 2

Create "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 directory

  • get_template_directory_uri(): url path to parent Theme directory

本文标签: post thumbnailsShow a default image