WordPress has_post_thumbnail() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Returns true or false

Definition

In WordPress, has_post_thumbnail() checks if a post has a featured image. It returns true if it has one, and false if it does not.

Use it before you show the image. This way your theme does not print empty tags or a broken layout for posts that have no featured image.

Syntax

A few ways to call has_post_thumbnail().

WordPress
<?php
if ( has_post_thumbnail() ) {
  the_post_thumbnail();
}
CallWhat it does
has_post_thumbnail()Check the current post.
has_post_thumbnail( 15 )Check the post with ID 15.
has_post_thumbnail( get_the_ID() )The same as the first call, written in full.
has_post_thumbnail( $post )Pass a WP_Post object.

It returns a boolean. It does not print anything. Use it in an if.

Full declaration
WordPress
has_post_thumbnail( int|WP_Post $post = null ): bool

Parameters

ParameterDescription
$postint|WP_Post|null. A post ID or post object. Default is null, which uses the current post.

Return Value

Return ValueDescription
trueThe post has a featured image.
falseThe post has no featured image, or the post does not exist.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Check the current post

Print a message that tells you if the post has a featured image.

WordPress
<?php
if ( has_post_thumbnail() ) {
  echo 'This post has a featured image.';
} else {
  echo 'This post has no featured image.';
}

How It Works

  1. has_post_thumbnail() checks the current post.
  2. It returns true, so the first message prints.
  3. With no featured image, the else message prints instead.

Example 2 — Check, then show the image

The most common pattern. Show the image only if it exists.

WordPress
<?php if ( has_post_thumbnail() ) : ?>
  <div class="post-image">
    <?php the_post_thumbnail( 'large' ); ?>
  </div>
<?php endif; ?>

How It Works

  1. If the post has no image, the whole div is skipped.
  2. No empty div is left on the page.
  3. the_post_thumbnail() prints the img tag.

Example 3 — Use a fallback image

Show a default image from your theme when the post has none.

WordPress
<?php
if ( has_post_thumbnail() ) {
  the_post_thumbnail( 'medium' );
} else {
  echo '<img src="' . esc_url( get_template_directory_uri() . '/images/default.jpg' ) . '" alt="">';
}

How It Works

  1. When there is no featured image, the else block runs.
  2. get_template_directory_uri() gives the theme folder URL.
  3. esc_url() makes the URL safe to print.

Frequently Asked Questions

It checks if a post has a featured image. It returns true or false.
To avoid empty markup. If you print a wrapper div or link with no image inside, the layout looks broken.
Yes. Pass the ID, for example has_post_thumbnail( 15 ).
Common causes: the post has no featured image set, or the theme does not support post-thumbnails.
Yes, if you pass a post ID or post object.

Browse all thumbnail functions

Open the functions index for simple guides with examples and FAQs.

Thumbnail Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful