Deluxe Blog Tips About Projects

How To Get Post Thumbnail URL In WordPress

How To Get Post Thumbnail URL In WordPress

WordPress has had post thumbnail feature since version 2.9. But untill now, there isn’t offical function to get post thumbnail url. The get_the_post_thumbnail() function is very close, but it only returns HTML code. Of course we can parse this code using regular expression to get thumbnail url. But we have another better solution as the following:

if (has_post_thumbnail()) {
    $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail_name');
    echo $thumb[0]; // thumbnail url
}

There’s one thing that I think useful: the post thumbnail actually is one attached image to post. Therefore, we can use wp_get_attachment_image_src() to get its url.

Subscribe to the Newsletter

Subscribe to get my latest content by email. I won't send you spam. Unsubscribe at any time.