How to get the post thumbnail URL in WordPress
July 12, 2018
how to get the post thumbnail URL in WordPress. 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.
1 2 3 4 5 6 7 8 9 |
// 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 get thumbnail url $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]; |
We Used Two functions :
1.
1 |
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.
2.
1 |
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.
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.
Thank you for comments