WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/has post thumbnail

Description

Returns a boolean if a post has a Featured Image (formerly known as Post Thumbnail) attached (true) or not (false).

Note: To enable featured images / post thumbnails, the current theme must include add_theme_support( 'post-thumbnails' ); in its functions.php file. See also Post Thumbnails.

Usage

 <?php has_post_thumbnail$post_id ); ?> 

Parameters

$post_id
(integer) (optional) Post ID.
Default: 'ID', the post ID.

Examples

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" />';
}
?>

Note The above code apparently fails in some instances and the below code is "recommended"

<?php 

if ( '' != get_the_post_thumbnail() ) {
    // some code
} else {
    // some code
}


?>

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().

Changelog

Source File

has_post_thumbnail() is located in wp-includes/post-thumbnail-template.php.

Related

Post Thumbnails: has_post_thumbnail(), the_post_thumbnail(), get_post_thumbnail_id(), get_the_post_thumbnail(), add_image_size(), set_post_thumbnail_size()

See also index of Function Reference and index of Template Tags.