WordPress the_post_thumbnail() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Prints the featured image

Definition

In WordPress, the_post_thumbnail() prints the featured image of the current post. It outputs an img tag. It is the quickest way to show a featured image in a theme.

To get the HTML as a string instead of printing it, use get_the_post_thumbnail().

Syntax

A few ways to call the_post_thumbnail().

WordPress
<?php
the_post_thumbnail( 'medium' );
CallWhat it does
the_post_thumbnail()Print the image at the default post-thumbnail size.
the_post_thumbnail( 'medium' )Print the image at the medium size.
the_post_thumbnail( array( 300, 200 ) )Print the image at a custom width and height.
the_post_thumbnail( 'large', array( 'class' => 'hero' ) )Add a CSS class to the img tag.

It prints the image and returns nothing. It works on the current post, so use it inside The Loop.

Full declaration
WordPress
the_post_thumbnail( string|int[] $size = 'post-thumbnail', string|array $attr = '' ): void

Parameters

ParameterDescription
$sizestring|int[]. An image size name, or an array of width and height. Default is 'post-thumbnail'.
$attrstring|array. Extra attributes for the img tag, such as class, alt, and loading. Default is empty.

Return Value

Return ValueDescription
voidThe featured image img tag is printed. Nothing is printed if the post has no featured image.

Image Sizes

The $size value can be any of these.

SizeWhat it means
'thumbnail'Small square image. Default 150 x 150, cropped. Set in Settings > Media.
'medium'Default max 300 x 300. Keeps the shape of the image.
'large'Default max 1024 x 1024. Keeps the shape of the image.
'full'The original image you uploaded.
'post-thumbnail'The theme's own size. A theme sets it with set_post_thumbnail_size(). This is the default.
array( 300, 200 )A custom width and height in pixels. WordPress picks the closest image size.

WordPress Examples

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

Example 1 — Turn on thumbnails and print one

Enable the feature in your theme, then print the image in a template.

WordPress
<?php
// functions.php
function mytheme_setup() {
  add_theme_support( 'post-thumbnails' );
}
add_action( 'after_setup_theme', 'mytheme_setup' );

// single.php
if ( has_post_thumbnail() ) {
  the_post_thumbnail();
}

How It Works

  1. add_theme_support( 'post-thumbnails' ) adds the Featured image box in the editor.
  2. has_post_thumbnail() checks that an image is set.
  3. the_post_thumbnail() prints it at the theme's default size.

Example 2 — Pick an image size

Use a size name, or an array of width and height.

WordPress
<?php
the_post_thumbnail( 'thumbnail' );
the_post_thumbnail( 'medium' );
the_post_thumbnail( 'full' );
the_post_thumbnail( array( 300, 200 ) );

How It Works

  1. A size name picks one of the saved image sizes.
  2. 'full' uses the original upload.
  3. An array asks for a custom width and height in pixels. The sample output is shortened with ....

Example 3 — Add class, alt, and lazy loading

Use the second argument to set attributes on the img tag.

WordPress
<?php
the_post_thumbnail(
  'large',
  array(
    'class'   => 'hero-image',
    'alt'     => 'Hero image for this post',
    'loading' => 'lazy',
  )
);

How It Works

  1. The first argument is the size.
  2. The second argument is an array of attributes.
  3. If you do not set alt, WordPress uses the alt text saved with the image in the Media Library.

Frequently Asked Questions

It prints the featured image of the current post as an img tag.
The post has no featured image, or the theme does not support post-thumbnails. Add add_theme_support( 'post-thumbnails' ) to functions.php.
post-thumbnail. A theme sets it with set_post_thumbnail_size(). If it is not set, it is the same as thumbnail.
the_post_thumbnail() prints the image. get_the_post_thumbnail() returns it as a string.
Yes, if you wrap the image in other HTML. This avoids empty wrappers when a post has no image.

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