Get the Post Thumbnail URL in WordPress

In this tutorial, we will learn simple snippet to get Post thumbnail URL in WordPress to display image with own <img> markup tag.

To display post thumbnail image generally function the_post_thumbnail() is used but this display image with<img> tag.

But here we are going to discuss how to get post thumbnail URL in WordPress. This URL can be used with own <img> tag. The other advantage is we can set class to the img tag.

Simple and easy snippet shown below can be used in loop to get post thumbnail URL .

<?php 
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'thumbnail-size', true);
echo $image_url[0];  
?>

We just need to pass value in place thumbnail-size argument used in above snippet. Thumbnail size are as follows: full, large, medium and thumbnail.

So we can put argument value as per required. get_post_thumbnail_id function is used to get post thumbnail id and that is passed to wp_get_attachment_image_src function to get image URL in array and then display with echo.

Leave a Comment