get_the_post_thumbnail_url( int|WP_Post $post = null, string|array $size = 'post-thumbnail' )
Return the post thumbnail URL.
Description Description
Parameters Parameters
Return Return
(string|false) Post thumbnail URL or false if no URL is available.
Source Source
File: wp-includes/post-thumbnail-template.php
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; } return wp_get_attachment_image_url( $post_thumbnail_id, $size ); }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
4.4.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Don’t ignore the first parameter.
Proper usage of `get_the_post_thumbnail_url()` inside the loop:
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:Expand full source codeCollapse full source code
Proper usage of `
get_the_post_thumbnail_url()
` outside 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.
To display the featured image with the alt tag use something like this
Expand full source codeCollapse full source code