I see many methods of showing thumbnails in WordPress, but I’m not immediately sure how I could get only the path to a post’s thumbnail rather than the html-ready code generated by functions like the_post_thumbnail() and get_the_post_thumbnail().

What methods are available to me to get only the path of the thumbnail (to set it as a bgimage) rather than the <img /> tag? Do I only have the option of parsing the results of the get_ method or is there an easier way?

4 s
4

Thumbnail is essentially attachment so you can approach from that side – lookup ID with get_post_thumbnail_id() and fetch data with wp_get_attachment_image_src(), like this:

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

(source)

Leave a Reply

Your email address will not be published. Required fields are marked *