get_the_post_thumbnail_url

Home » Functions » get_the_post_thumbnail_url

Today, we will be discussing the get_the_post_thumbnail_url function in WordPress. This function allows you to retrieve the URL of the featured image attached to a particular post.

The featured image is also known as the post thumbnail. It is an important aspect of WordPress because it allows you to display an image that represents the content of the post. By using this function, you can quickly retrieve the URL of the featured image and use it in your WordPress theme.

Here is an example of how to use the get_the_post_thumbnail_url function in your WordPress theme:

<?php 
$image_url = get_the_post_thumbnail_url( $post_id, $size, $attr ); 
echo '<img src="' . $image_url . '" alt="Post thumbnail" />';
?>

In this example, we are using the get_the_post_thumbnail_url function to retrieve the URL of the featured image for a specific post. We are then using this URL to display the image on the page using the img HTML tag.

The function accepts three parameters:

  • $post_id (optional): The ID of the post you want to retrieve the featured image for. If not specified, the ID of the current post is used.
  • $size (optional): The size of the image you want to retrieve. This can be the name of a registered image size or an array of width and height values in pixels.
  • $attr (optional): An array of attributes to apply to the HTML img tag.

Overall, the get_the_post_thumbnail_url function is a useful tool in WordPress that allows you to easily retrieve the URL of the featured image attached to a particular post.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.