Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get the post thumbnail


Gets the Featured Image (formerly called Post Thumbnail) as set in post's or page's edit screen and returns an HTML image element representing a Featured Image, if there is any, otherwise an empty string.

Note: To enable Post Thumbnails, the current theme must include add_theme_support( 'post-thumbnails' ); in its functions.php file. See also Post Thumbnails.


 <?php echo get_the_post_thumbnail$post_id$size$attr ); ?> 


(integer) (Optional) Post ID.
Default: Post ID
(string/array) (Optional) Either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32).
Default: 'post-thumbnail'
(string/array) (optional) Query string or array of attributes. See wp_get_attachment_image.
Default: None
$default_attr = array(
	'src'	=> $src,
	'class'	=> "attachment-$size",
	'alt'	=> trim( strip_tags( $attachment->post_excerpt ) ),
	'title'	=> trim( strip_tags( $attachment->post_title ) ),

Return Values

Returns a string containing an HTML image element, or an empty string if no post thumbnail is found.


Sample Usage

<?php $pages = get_pages( array( 'child_of' => 1 ) ); ?> 
	<?php foreach ( $pages as $page ) : ?>
			<?php echo get_the_post_thumbnail( $page->ID, 'thumbnail' ); ?>
			<h1><?php echo apply_filters( 'the_title', $page->post_title, $page->ID ); ?></h1>
			<?php echo apply_filters( 'the_content', $page->post_content ); ?>
	<?php endforeach; ?>

Thumbnail Sizes

The default image sizes of WordPress are "thumbnail", "medium", "large" and "full" (the size of the image you uploaded). These image sizes can be configured in the WordPress Administration Media panel under Settings > Media. Themes may also add "post-thumbnail". This is how you can use these default sizes with get_the_post_thumbnail():

// without parameter -> Post Thumbnail (as set by theme using set_post_thumbnail_size())
get_the_post_thumbnail( $post_id );                   

get_the_post_thumbnail( $post_id, 'thumbnail' );      // Thumbnail (Note: different to Post Thumbnail)
get_the_post_thumbnail( $post_id, 'medium' );         // Medium resolution
get_the_post_thumbnail( $post_id, 'large' );          // Large resolution
get_the_post_thumbnail( $post_id, 'full' );           // Original resolution

get_the_post_thumbnail( $post_id, array( 100, 100) ); // Other resolutions

Register new image sizes for Post Thumbnails with: add_image_size().
To set the default size for Post Thumbnails see: set_post_thumbnail_size().

Post Thumbnail Linking to the Post Permalink

This example shows the 5 latest Post Thumbnails linked to their Post permalink.

$thumbnails = get_posts( 'numberposts=5' );
foreach ( $thumbnails as $thumbnail ) {
	if ( has_post_thumbnail( $thumbnail->ID ) ) {
		echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
		echo get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' );
		echo '</a>';

Post Thumbnail Linking to large Image Size

This example links to the "large" Post Thumbnail image size and must be used within The Loop.

if ( has_post_thumbnail() ) {
	$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
	echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '" >';
	echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); 
	echo '</a>';

Styling Post Thumbnails

Post Thumbnails are given a class "wp-post-image". They also get a class depending on the size of the thumbnail being displayed You can style the output with these CSS selectors:


You can also give Post Thumbnails their own class.
Give the Post Thumbnail a class "alignleft".

<?php echo get_the_post_thumbnail( $post_id, 'thumbnail', array( 'class' => 'alignleft' ) ); ?>


If the required add_theme_support( 'post-thumbnails' ); in the current theme's functions.php file is attached to a hook, it must be must be called before the init hook is fired. The init hook may be too late for some features. If attached to a hook, it must be after_setup_theme.

Change Log

Since: 2.9.0

Source File

get_the_post_thumbnail() is located in wp-includes/post-thumbnail-template.php.

External Resources


Post Thumbnails:

See also index of Function Reference and index of Template Tags.