get_the_post_thumbnail_url()WP 4.4.0

Gets the thumbnail URL specified for the post. If there is no post thumbnail, it returns false.

1 time — 0.001901 sec (very slow) | 50000 times — 18 sec (slow) | PHP 7.0.5, WP 4.4.2
Hooks from the function

Return

String|false. Post thumbnail URL or false if no image is available. If $size does not match any registered image size, the original image URL will be returned.

Usage

get_the_post_thumbnail_url( $post, $size );
$post(number/WP_Post)
ID/object of post.
Default: current post
$size(string/array)

The name of the registered thumbnail size: thumbnail, medium, large or full, or image size as an array with width and height: [ 200, 300 ].

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.

Default: 'post-thumbnail'

Examples

0

#1 Get the URL of the post thumbnail

echo get_the_post_thumbnail_url( 6732, 'thumbnail' );

// it will return
// http://wp-kama.com/wp-content/uploads/2016/03/post-meta-fields4-80x80.png
0

#2 Don’t ignore the first parameter.

Proper usage of this function inside the loop:

if ( have_posts() ) :
	while ( have_posts() ) :
		the_post();

		// grab the url for the full size featured image
		$featured_img_url = get_the_post_thumbnail_url( get_the_ID(), 'full' );

		// link thumbnail to full size image for use with lightbox
		echo '<a href="'. esc_url( $featured_img_url ) .'" rel="lightbox">';
			the_post_thumbnail( 'thumbnail' );
		echo '</a>';
	endwhile;
endif;

Changelog

Since 4.4.0 Introduced.

get_the_post_thumbnail_url() code WP 6.4.3

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 );
}