How to get the post thumbnail URL in WordPress

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.

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.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.