Home WordPress Thumbnails Functions get_the_post_thumbnail() WordPress get_the_post_thumbnail() Function Definition In WordPress, get_the_post_thumbnail() returns the HTML of the featured image of a post. The result is an img tag as a string.
It does not print. Use it when you want to put the image inside your own HTML, such as a link or a card. To print the image straight away, use the_post_thumbnail().
Syntax A few ways to call get_the_post_thumbnail().
<?php
echo get_the_post_thumbnail( null, 'medium' ); Call What it does get_the_post_thumbnail()Return the image of the current post at the default size. get_the_post_thumbnail( 15 )Return the image of the post with ID 15. get_the_post_thumbnail( null, 'medium' )Current post, medium size. get_the_post_thumbnail( null, 'large', array( 'class' => 'hero' ) )Add a CSS class to the img tag.
It returns a string. You must echo it. The string is empty if the post has no featured image.
Full declaration get_the_post_thumbnail( int|WP_Post $post = null, string|int[] $size = 'post-thumbnail', string|array $attr = '' ): string Parameters Parameter Description $postint|WP_Post|null. A post ID or post object. Default is null, which uses the current post. $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 string The img tag HTML of the featured image. empty string The post has no featured image, or the post does not exist.
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. Return and print the image 2. Set a size and attributes 3. Wrap the image in a link Example 1 — Return and print the image Save the image HTML in a variable, then print it.
<?php
$image = get_the_post_thumbnail();
echo $image; <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 get_the_post_thumbnail() builds the img tag for the current post.WordPress adds helpful classes such as wp-post-image. echo prints the string.Example 2 — Set a size and attributes Choose the medium size and add a class, an alt, and lazy loading.
<?php
echo get_the_post_thumbnail(
get_the_ID(),
'medium',
array(
'class' => 'card-img',
'alt' => 'Cover image',
'loading' => 'lazy',
)
); <img width="300" height="200" src="https://example.com/wp-content/uploads/2026/11/photo-300x200.jpg" class="attachment-medium size-medium wp-post-image card-img" alt="Cover image" decoding="async" loading="lazy" /> How It Works The second argument is the size name. The third argument is an array of attributes for the img tag. The class value is added to the classes WordPress already sets. Example 3 — Wrap the image in a link Check first, then put the image inside a link to the post.
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>">
<?php echo get_the_post_thumbnail( null, 'medium' ); ?>
</a>
<?php endif; ?> <a href="https://example.com/hello-world/">
<img width="300" height="200" src="https://example.com/wp-content/uploads/2026/11/photo-300x200.jpg" class="attachment-medium size-medium wp-post-image" alt="" decoding="async" />
</a> How It Works has_post_thumbnail() makes sure there is an image, so no empty link is printed.get_the_post_thumbnail() returns the image, so it can sit inside the a tag.This is why you pick get_ over the_: you control the HTML around it. Frequently Asked Questions What does get_the_post_thumbnail() do? It returns the featured image of a post as an img tag string. It does not print it.
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, so you can wrap it in your own HTML.
What is returned when there is no featured image? An empty string. Check with has_post_thumbnail() first if you add HTML around the image.
Can I use a custom size? Yes. Pass an array such as array( 300, 200 ), or a size name registered with add_image_size().
How do I add a class or alt text? Pass an array as the third argument, for example array( 'class' => 'hero', 'alt' => 'Cover' ).
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.
13 people found this page helpful
Helpful Share Copy link Suggestion