avatar 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.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com