Using WordPress ‘get_the_post_thumbnail_url()’ PHP function

The get_the_post_thumbnail_url() WordPress PHP function returns the URL of the post’s thumbnail image.

Usage

To use the function, simply pass in the post ID or WP_Post object as the first parameter, and optionally the registered image size to retrieve the source for or a flat array of height and width dimensions as the second parameter. The function then returns the URL of the post’s thumbnail image.

For example:

$post_id = 1;
$thumbnail_url = get_the_post_thumbnail_url($post_id, 'medium');
echo $thumbnail_url;

This code will retrieve the URL for the ‘medium’ size of the post thumbnail for the post with ID 1 and print it to the page.

Parameters

  • $post (int|WP_Post) Optional Post ID or WP_Post object. Default is global $post.
  • $size (string|int) Optional Registered image size to retrieve the source for or a flat array of height and width dimensions. Default ‘post-thumbnail’.

More information

See WordPress Developer Resources: get_the_post_thumbnail_url()

This function was introduced in WordPress 2.9.0 and has not been deprecated as of WordPress 5.7.1. The function is located in the wp-includes/post-thumbnail-template.php file and is related to the the_post_thumbnail() and get_the_post_thumbnail() functions.

Examples

Retrieving the URL of the post’s thumbnail image

This example shows how to retrieve the URL of the post’s thumbnail image using the get_the_post_thumbnail_url() function.

$post_id = 1;
$thumbnail_url = get_the_post_thumbnail_url($post_id);
echo $thumbnail_url;

This code will retrieve the URL for the ‘post-thumbnail’ size of the post thumbnail for the post with ID 1 and print it to the page.

Retrieving a custom size of the post’s thumbnail image

This example shows how to retrieve a custom size of the post’s thumbnail image using the get_the_post_thumbnail_url() function.

$post_id = 1;
$thumbnail_url = get_the_post_thumbnail_url($post_id, array(500, 500));
echo $thumbnail_url;

This code will retrieve the URL for a custom size of 500×500 pixels of the post thumbnail for the post with ID 1 and print it to the page.

Retrieving the URL of the post’s thumbnail image outside the loop

This example shows how to retrieve the URL of the post’s thumbnail image outside the loop using the get_the_post_thumbnail_url() function.

$post_id = 1;
$post = get_post($post_id);
$thumbnail_url = get_the_post_thumbnail_url($post->ID);
echo $thumbnail_url;

This code will retrieve the URL for the ‘post-thumbnail’ size of the post thumbnail for the post with ID 1 and print it to the page.

Retrieving the URL of a custom size of the post’s thumbnail image outside the loop

This example shows how to retrieve the URL of a custom size of the post’s thumbnail image outside the loop using the get_the_post_thumbnail_url() function.

$post_id = 1;
$post = get_post($post_id);
$thumbnail_url = get_the_post_thumbnail_url($post->ID, array(500, 500));
echo $thumbnail_url;

This code will retrieve the URL for a custom size of 500×500 pixels of the post thumbnail for the post with ID 1 and print it to