Getting the post thumbnail URL in WordPress with the_post_thumbnail_url

The the_post_thumbnail_url function in WordPress is used to retrieve the URL of the featured image (also known as post thumbnail) for a specific post. This function can be useful for displaying the featured image in a custom location within a WordPress theme or for using the image URL in a custom function or plugin.

By using the the_post_thumbnail_url function, developers can easily access the URL of the featured image without having to manually retrieve it from the post object. This can streamline the process of displaying the featured image and make it easier to work with within the WordPress environment.

Parameters accepted by the the_post_thumbnail_url function

  • $size (string|int[]), optional. Default value: ‘post-thumbnail’. Description: Image size to use. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default ‘post-thumbnail’.

The function does not return a value.

Examples

How to get the post thumbnail URL in WordPress

Use the_post_thumbnail_url function to retrieve the URL of the current post’s thumbnail image.

<?php
$post_thumbnail_url = the_post_thumbnail_url();
if ($post_thumbnail_url) {
 echo 'The post thumbnail URL is: ' . $post_thumbnail_url;
} else {
 echo 'This post does not have a thumbnail image.';
}
?>

How to get the post thumbnail URL with a specific size in WordPress

Use the_post_thumbnail_url function with a specific image size parameter to retrieve the URL of the current post’s thumbnail image with the specified size.

<?php
$thumbnail_size = 'medium'; // Specify the desired image size
$post_thumbnail_url = the_post_thumbnail_url($thumbnail_size);
if ($post_thumbnail_url) {
 echo 'The post thumbnail URL with size ' . $thumbnail_size . ' is: ' . $post_thumbnail_url;
} else {
 echo 'This post does not have a thumbnail image with size ' . $thumbnail_size . '.';
}
?>

How to display a default image if post thumbnail is not available in WordPress

Use the_post_thumbnail_url function to retrieve the URL of the current post’s thumbnail image, and display a default image if the post does not have a thumbnail.

<?php
$post_thumbnail_url = the_post_thumbnail_url();
if ($post_thumbnail_url) {
 echo 'The post thumbnail URL is: ' . $post_thumbnail_url;
} else {
 $default_image_url = 'path/to/default-image.jpg'; // Specify the default image URL
 echo 'This post does not have a thumbnail image. Displaying default image: <img src="' . $default_image_url . '">';
}
?>

Conclusion

In conclusion, the the_post_thumbnail_url function is an effective feature for retrieving the URL of the featured image in WordPress. This function provides a simple and efficient way to access and display the featured image of a post or page. By using this function, developers can easily incorporate the featured image into their WordPress themes and plugins, enhancing the visual appeal and user experience of their websites.

The the_post_thumbnail_url function is a valuable asset for WordPress developers, offering a convenient solution for working with featured images. Its straightforward syntax and versatility make it a go-to choice for accessing and displaying featured images in WordPress. Whether you are building a custom theme or creating a plugin, the the_post_thumbnail_url function can streamline the process of incorporating featured images into your WordPress projects.

Related WordPress Functions