Returns the post thumbnail URL.
Parameters
Source
function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
$post_thumbnail_id = get_post_thumbnail_id( $post );
if ( ! $post_thumbnail_id ) {
return false;
}
$thumbnail_url = wp_get_attachment_image_url( $post_thumbnail_id, $size );
/**
* Filters the post thumbnail URL.
*
* @since 5.9.0
*
* @param string|false $thumbnail_url Post thumbnail URL or false if the post does not exist.
* @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`.
* @param string|int[] $size Registered image size to retrieve the source for or a flat array
* of height and width dimensions. Default 'post-thumbnail'.
*/
return apply_filters( 'post_thumbnail_url', $thumbnail_url, $post, $size );
}
Hooks
- apply_filters( ‘post_thumbnail_url’,
string|false $thumbnail_url ,int|WP_Post|null $post ,string|int[] $size ) Filters the post thumbnail URL.
Changelog
Version | Description |
---|---|
4.4.0 | Introduced. |
Don’t ignore the first parameter.
Proper usage of `get_the_post_thumbnail_url() ` inside the loop:
It’s worth to note that, if you upload a smaller image (let’s say, a 600px wide) and use this to fetch a specific larger image (let’s say, a 1920px wide for your cover), it will return the original image instead (which is smaller than what you need) instead of returning false.
If you need to fallback to another image in case the specified file doesn’t exist, you can look into wp_get_attachment_image_src instead, and check for the width or height of the image.
Downvoted the example posted from @thelilmercoder as it is not proper usage of this function.
Proper usage of `
get_the_post_thumbnail_url()
` inside the loop:Proper usage of `
get_the_post_thumbnail_url()
` outside the loop:get_the_post_thumbnail_url()
inside the loop:. The first argument ofget_the_post_thumbnail_url()
should be the post ID or object, not the image size. You might want to addget_the_ID()
before ‘full’ argument to get the full-size featured image URL.To display the featured image with the alt tag use something like this
get_the_post_thumbnail_url()
function return a thumbnail URL as string or false on error. So, you can’t access media ID through$thumbnail->ID
. This code has error.$thumbnail
will return the URL (string), not the image object. This won’t work. You could try to useattachment_url_to_postid( $thumbnail )
to try and get the attachment ID.