has_post_thumbnail – WordPress function

The has_post_thumbnail is a WordPress standard function that added to WordPress after version 2.9.0. This function checks if the specified post has a thumbnail image. Conditional tag.

Description

As we already mentioned, the has_post_thumbnail checks if the post has a thumbnail image.

has_post_thumbnail( $post = null );

What does the has_post_thumbnail returns?

It returns whether the post has an attached thumbnail image – true/false. If the post has a thumbnail the function returns – true, if it doesn’t – false.

Parameters

$post
(int|WP_Post) (Optional) Post ID or WP_Post object. The default parameter is global $post.
Default value: null

Source

The source of this function is this file – wp-includes/post-thumbnail-template.php

 has_post_thumbnail WordPress function

Useful example

This example checks if a post has a thumbnail image attached to it. If there isn’t a post thumbnail image, then we will display the default image(we will load the file into the images folder of our theme).

Important Note

In order for a post to be able to define a thumbnail image, you need to activate this feature with the function – add_theme_support (‘post-thumbnails’); from the theme funсtions.php file.

This function works based on: get_post_thumbnail_id ()

1 Comment

Leave a comment