How to Extract the Thumbnail Source in WordPress

Snippets by IsItWP

Are you looking for a way to extract the source of the thumbnail image? While there’s probably a plugin for this, we have created a quick code snippet that you can use to extract the thumbnail source in WordPress.

It could be needed in a situation where the_post_thumbnail() function is out of place, which also adds the img tag. The following code creates a variable $image_url which you can use wherever in the document.

This code is meant to be put in an actual WordPress template file, and can be used in different loops, where you want to use the source of the individual thumbnails.

This snippet gives you the actual source of the thumbnail instead of printing it in an image element.


All you have to do is add this code to your theme’s single.php file or in a site-specific plugin:

$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id);
$image_url = $image_url[0];

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 24 best WordPress education themes and how to secure your WordPress forms with password protection.

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!