Retrieves the post thumbnail.
Description
When a theme adds ‘post-thumbnail’ support, a special ‘post-thumbnail’ image size is registered, which differs from the ‘thumbnail’ image size managed via the Settings > Media screen.
When using the_post_thumbnail() or related functions, the ‘post-thumbnail’ image size is used by default, though a different size can be specified instead as needed.
Parameters
$post
int|WP_Postoptional- Post ID or WP_Post object. Default is global
$post
.Default:
null
$size
string|int[]optional- Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default
'post-thumbnail'
.Default:
'post-thumbnail'
$attr
string|arrayoptional- Query string or array of attributes.
Default:
''
Source
function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
$post = get_post( $post );
if ( ! $post ) {
return '';
}
$post_thumbnail_id = get_post_thumbnail_id( $post );
/**
* Filters the post thumbnail size.
*
* @since 2.9.0
* @since 4.9.0 Added the `$post_id` parameter.
*
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
* @param int $post_id The post ID.
*/
$size = apply_filters( 'post_thumbnail_size', $size, $post->ID );
if ( $post_thumbnail_id ) {
/**
* Fires before fetching the post thumbnail HTML.
*
* Provides "just in time" filtering of all filters in wp_get_attachment_image().
*
* @since 2.9.0
*
* @param int $post_id The post ID.
* @param int $post_thumbnail_id The post thumbnail ID.
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
*/
do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
if ( in_the_loop() ) {
update_post_thumbnail_cache();
}
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
/**
* Fires after fetching the post thumbnail HTML.
*
* @since 2.9.0
*
* @param int $post_id The post ID.
* @param int $post_thumbnail_id The post thumbnail ID.
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
*/
do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
} else {
$html = '';
}
/**
* Filters the post thumbnail HTML.
*
* @since 2.9.0
*
* @param string $html The post thumbnail HTML.
* @param int $post_id The post ID.
* @param int $post_thumbnail_id The post thumbnail ID, or 0 if there isn't one.
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
* @param string|array $attr Query string or array of attributes.
*/
return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
}
Hooks
- do_action( ‘begin_fetch_post_thumbnail_html’,
int $post_id ,int $post_thumbnail_id ,string|int[] $size ) Fires before fetching the post thumbnail HTML.
- do_action( ‘end_fetch_post_thumbnail_html’,
int $post_id ,int $post_thumbnail_id ,string|int[] $size ) Fires after fetching the post thumbnail HTML.
- apply_filters( ‘post_thumbnail_html’,
string $html ,int $post_id ,int $post_thumbnail_id ,string|int[] $size ,string|array $attr ) Filters the post thumbnail HTML.
- apply_filters( ‘post_thumbnail_size’,
string|int[] $size ,int $post_id ) Filters the post thumbnail size.
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:
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()
:Register new image sizes for Post Thumbnails with:
add_image_size()
.To set the default size for Post Thumbnails see:
set_post_thumbnail_size()
.Sample Usage
Post Thumbnail Linking to the Post Permalink
This example shows the 5 latest Post Thumbnails linked to their Post permalink.
Remove width and height attributes from the returning html
get_the_post_thumbnail
function produces html withimg
tag which includes width, height, src, class, alt and title attributes. The following code snippet is useful if someone wish to remove width and height attribute.This function provides
'post_thumbnail_html'
filter before returning the html.You can set more than just classes –
If you want to optimize your SrcSet values you can do something like this
etc.
Normally in a grid, the srcset can go 100vw – which is sometimes way too big –
this method lets you control that.
Post Thumbnail Linking to large Image Size
This example links to the “large” Post Thumbnail image size and must be used within The Loop.
See Also:
the_post_thumbnail()
When specifying a size e.g.
get_the_post_thumbnail(null, [300, 300])
, the image’s proportions are preserved and the width/height are maximum sizes. If the a dimension’s are smaller than specified, then the image returned will be the full size image.Example 1: Your image is 500px (width) by 1000px (height), the image returned will be 150px by 300px.
Example 2: Your image is 100px by 100px the the image size returned will be 100px by 100px
Post thumbnails in a listing context are often linked to the content they’re representing. Instead of adding that every time, here is a filter you can add that will do it automatically based on an `$attr` passed in.
Just “add the above to your functions.php file” (there’s probably a better place for it) and use it like this in a template file:
To give class to the featured image post thumbnail
To get full featured image