has_post_thumbnail() WordPress Function

has_post_thumbnail() is a function that checks whether a post has a thumbnail image associated with it. If a post does have a thumbnail image, the function will return true. Otherwise, it will return false. This function is often used in conjunction with the the_post_thumbnail() function, which displays the thumbnail image.

has_post_thumbnail( int|WP_Post $post = null ) #

Determines whether a post has an image attached.


Description

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.


Top ↑

Parameters

$post

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

Default value: null


Top ↑

Return

(bool) Whether the post has an image attached.


Top ↑

Source

File: wp-includes/post-thumbnail-template.php

function has_post_thumbnail( $post = null ) {
	$thumbnail_id  = get_post_thumbnail_id( $post );
	$has_thumbnail = (bool) $thumbnail_id;

	/**
	 * Filters whether a post has a post thumbnail.
	 *
	 * @since 5.1.0
	 *
	 * @param bool             $has_thumbnail true if the post has a post thumbnail, otherwise false.
	 * @param int|WP_Post|null $post          Post ID or WP_Post object. Default is global `$post`.
	 * @param int|false        $thumbnail_id  Post thumbnail ID or false if the post does not exist.
	 */
	return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
}


Top ↑

Changelog

Changelog
VersionDescription
4.4.0$post can be a post ID or WP_Post object.
2.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.