the_post_thumbnail_url()WP 4.4.0

Outputs the URL of the current post's thumbnail. If there is no thumbnail, it returns NULL (nothing will be output). Used in the WordPress loop.

Before outputting, the link is sanitized using esc_url().

1 time — 0.0082059 sec (very slow) | 50000 times — 3.35 sec (fast) | PHP 8.2.25, WP 6.8.1

No Hooks.

Returns

null.

Usage

<?php the_post_thumbnail_url( $size ); ?>
$size(string/array)
The name of the registered thumbnail size or image sizes as an array with width and height: array(200, 300).
Default: 'post-thumbnail'

Examples

0

#1 Display the URL of the post thumbnail

the_post_thumbnail_url( 'thumbnail' );
// outputs
// http://example.com/wp-content/uploads/2016/03/post-meta-fields4-80x80.png
0

#2 Background thumbnail for DIV element

<div class="post" style="background-image: url(<?php the_post_thumbnail_url(); ?>)"></div>

Changelog

Since 4.4.0 Introduced.

the_post_thumbnail_url() code WP 7.0

function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
	$url = get_the_post_thumbnail_url( null, $size );

	if ( $url ) {
		echo esc_url( $url );
	}
}