How to Get the Post Thumbnail URL in WordPress

Some people recently asked us how to get the post thumbnail URL in WordPress. The goal here is to just get the post thumbnail URL and then use it with their own markup. In this article, we will show you how to get the post thumbnail URL in WordPress.

Simply paste this code inside the loop code that you are writing.


$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
echo $thumb_url[0];

Make sure to replace thumbnail-size to whatever image size you want to get. Default sizes are thumbnail, medium, large, and full. You can also use add and use additional image sizes. If you are using a custom image size then make sure to regenerate thumbnails. You can also set a default fallback image for post thumbnails.

What this code does is that we first get the post thumbnail ID. Then we use that id to get the image path or URL using wp_get_attachment_image_src function, which returns an array that you can use with your own code.

Leave a Reply

Your email address will not be published. Required fields are marked *