Skip to content

How to get the post thumbnail URL in WordPress

Last updated on February 10, 2018

In this tutorial, i will show you how to get the post thumbnail URL in WordPress.Generally we use the_post_thumbnail(); function to display post thumbnail. But unfortunately there is no direct function in WordPress to get post thumbnail image url. So we use two functions ( get_post_thumbnail_id(), wp_get_attachment_image_src() ) here to get image URL of post and then use it.

simply paste this code in the loop or pass post ID to get_post_thumbnail_id() function, which will return the attachment id.Then we use that id to get the image URL using wp_get_attachment_image_src() function, which returns an array.

// keep it inside the loop.
$thumb_id = get_post_thumbnail_id(); 
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];

// by using post id 
$thumb_id = get_post_thumbnail_id($post_id); 
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];

Used functions

get_post_thumbnail_id($post_id), this will return post thumbnail id, and $post_id is optional If post id is null, the current post will be used.

wp_get_attachment_image_src($attachment_id, $size, $icon ) this function will return the array with values corresponding to the (0) url, (1) width, (2) height, and (3) scale of an image attachment.

That’s it.

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments