Home WordPress Thumbnails Functions the_post_thumbnail() WordPress the_post_thumbnail() Function 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().
<?php
the_post_thumbnail( 'medium' ); Call What 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 the_post_thumbnail( string|int[] $size = 'post-thumbnail', string|array $attr = '' ): void Parameters Parameter Description $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 Value Description void The 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.
Size What 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.
1. Turn on thumbnails and print one 2. Pick an image size 3. Add class, alt, and lazy loading Example 1 — Turn on thumbnails and print one Enable the feature in your theme, then print the image in a template.
<?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();
} <img width="1200" height="630" src="https://example.com/wp-content/uploads/2026/11/photo.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" /> How It Works add_theme_support( 'post-thumbnails' ) adds the Featured image box in the editor.has_post_thumbnail() checks that an image is set.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.
<?php
the_post_thumbnail( 'thumbnail' );
the_post_thumbnail( 'medium' );
the_post_thumbnail( 'full' );
the_post_thumbnail( array( 300, 200 ) ); <img width="150" height="150" src=".../photo-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" ... />
<img width="300" height="200" src=".../photo-300x200.jpg" class="attachment-medium size-medium wp-post-image" ... />
<img width="1200" height="800" src=".../photo.jpg" class="attachment-full size-full wp-post-image" ... />
<img width="300" height="200" src=".../photo-300x200.jpg" class="attachment-300x200 size-300x200 wp-post-image" ... /> How It Works A size name picks one of the saved image sizes. 'full' uses the original upload.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.
<?php
the_post_thumbnail(
'large',
array(
'class' => 'hero-image',
'alt' => 'Hero image for this post',
'loading' => 'lazy',
)
); <img width="1024" height="538" src="https://example.com/wp-content/uploads/2026/11/photo-1024x538.jpg" class="attachment-large size-large wp-post-image hero-image" alt="Hero image for this post" decoding="async" loading="lazy" /> How It Works The first argument is the size. The second argument is an array of attributes. If you do not set alt, WordPress uses the alt text saved with the image in the Media Library. Frequently Asked Questions What does the_post_thumbnail() do? It prints the featured image of the current post as an img tag.
Why does the_post_thumbnail() print nothing? The post has no featured image, or the theme does not support post-thumbnails. Add add_theme_support( 'post-thumbnails' ) to functions.php.
What is the default size? post-thumbnail. A theme sets it with set_post_thumbnail_size(). If it is not set, it is the same as thumbnail.
What is the difference between the_post_thumbnail() and get_the_post_thumbnail()? the_post_thumbnail() prints the image. get_the_post_thumbnail() returns it as a string.
Should I check with has_post_thumbnail() first? Yes, if you wrap the image in other HTML. This avoids empty wrappers when a post has no image.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion