Getting the featured image URL in WordPress with get_the_post_thumbnail_url

The WordPress get_the_post_thumbnail_url function retrieves the URL of the featured image (also known as the post thumbnail) for a specified post. This can be useful for displaying the featured image in a custom location or for using the URL in a custom function or template.

By using get_the_post_thumbnail_url, developers can easily access the URL of the featured image without having to manually retrieve it from the post object. This can streamline the development process and make it easier to work with featured images in WordPress themes and plugins.

Parameters accepted by get_the_post_thumbnail_url function

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

Value returned by get_the_post_thumbnail_url function

The function returns a string representing the post thumbnail URL, or false if no image is available. If the specified $size does not match any registered image size, the original image URL will be returned.

Examples

How to get the post thumbnail URL for the current post

<?php
$post_thumbnail_url = get_the_post_thumbnail_url();
echo $post_thumbnail_url;
?>

This code snippet retrieves the URL of the post thumbnail for the current post using the get_the_post_thumbnail_url function and then echoes the URL.

How to get the post thumbnail URL for a specific post

<?php
$post_id = 123; // Replace with the desired post ID
$post_thumbnail_url = get_the_post_thumbnail_url($post_id);
echo $post_thumbnail_url;
?>

This code snippet retrieves the URL of the post thumbnail for a specific post with ID 123 using the get_the_post_thumbnail_url function and then echoes the URL.

How to display a default image if no post thumbnail is set

<?php
$post_thumbnail_url = get_the_post_thumbnail_url();
if (empty($post_thumbnail_url)) {
 $default_image_url = 'path_to_default_image.jpg'; // Replace with the default image URL
 $post_thumbnail_url = $default_image_url;
}
echo $post_thumbnail_url;
?>

This code snippet first retrieves the URL of the post thumbnail for the current post using the get_the_post_thumbnail_url function. It then checks if the URL is empty, and if so, assigns a default image URL to $post_thumbnail_url before echoing it.

Conclusion

The get_the_post_thumbnail_url function is a powerful tool for retrieving the URL of the featured image of a WordPress post. It offers flexibility in terms of specifying the size and additional parameters, making it a versatile function for developers and designers. By using this function, you can easily access the URL of the featured image and incorporate it into your website or application. Whether you are building a custom theme or developing a plugin, the get_the_post_thumbnail_url function can streamline your development process and enhance the user experience.

Related WordPress Functions