has_post_thumbnail ( int|WP_Post $post = null )

Check if post has an image attached.

Return: (bool) Whether the post has an image attached.

Source file: wp-includes/post-thumbnail-template.php

View source


Parameters

$post

(int|WP_Post) (Optional) Post ID or WP_Post object. Default is global $post.

Default value: null



Source

function has_post_thumbnail( $post = null ) {
	return (bool) get_post_thumbnail_id( $post );
}


Changelog

  • Since: WordPress 2.9.0
  • Since: WordPress 4.4.0 $post can be a post ID or WP_Post object.

User Contributed Notes

  1. This example first checks if there is a Post Thumbnail (aka Featured Image) set for the current queried item. If there is a Post Thumbnail set, it returns the Post Thumbnail. If not, it echoes out a default image which should be located in the current theme’s image folder (assuming the folder is in the theme’s root directory).

    <?php
    // Must be inside a loop.
    
    if ( has_post_thumbnail() ) {
    	the_post_thumbnail();
    }
    else {
    	echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) 
    		. '/images/thumbnail-default.jpg" />';
    }
    ?>
    

    You can use set_post_thumbnail_size() to set a default size for your thumbnail. Alternatively, you can add new image sizes to the defaults by use add_image_size().

You must log in before being able to contribute a note.