How to get the post thumbnail URL in WordPress

wodpress tutorial
Wordpress Tutorial

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.

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

0 0 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Timothy J. Hykes
Timothy J. Hykes
4 years ago

This doesn’t help me at all.