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.
Instructions:
All you have to do is add this code to your theme’s single.php file or in a site-specific plugin:
<?php // EXTRACTING THE THUMBNAIL IMAGE SRC. $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