WordPress get_the_post_thumbnail() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns the image HTML

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().

WordPress
<?php
echo get_the_post_thumbnail( null, 'medium' );
CallWhat 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
WordPress
get_the_post_thumbnail( int|WP_Post $post = null, string|int[] $size = 'post-thumbnail', string|array $attr = '' ): string

Parameters

ParameterDescription
$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 ValueDescription
stringThe img tag HTML of the featured image.
empty stringThe post has no featured image, or the post does not exist.

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 — Return and print the image

Save the image HTML in a variable, then print it.

WordPress
<?php
$image = get_the_post_thumbnail();

echo $image;

How It Works

  1. get_the_post_thumbnail() builds the img tag for the current post.
  2. WordPress adds helpful classes such as wp-post-image.
  3. echo prints the string.

Example 2 — Set a size and attributes

Choose the medium size and add a class, an alt, and lazy loading.

WordPress
<?php
echo get_the_post_thumbnail(
  get_the_ID(),
  'medium',
  array(
    'class'   => 'card-img',
    'alt'     => 'Cover image',
    'loading' => 'lazy',
  )
);

How It Works

  1. The second argument is the size name.
  2. The third argument is an array of attributes for the img tag.
  3. The class value is added to the classes WordPress already sets.

Frequently Asked Questions

It returns the featured image of a post as an img tag string. It does not print it.
the_post_thumbnail() prints the image. get_the_post_thumbnail() returns it, so you can wrap it in your own HTML.
An empty string. Check with has_post_thumbnail() first if you add HTML around the image.
Yes. Pass an array such as array( 300, 200 ), or a size name registered with add_image_size().
Pass an array as the third argument, for example array( 'class' => 'hero', 'alt' => 'Cover' ).

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.

13 people found this page helpful