How to Get the Thumbnail URL Path

Here is a quick and easy way to extract the URL source for the thumbnail [of the featured img] of your post.

Inside the Loop

<?php
// Extracting the thumbnail source URL
$img_id = get_post_thumbnail_id();
$img_url = wp_get_attachment_image_src($img_id);
$img_url = $img_url[0];
?>

Outside the Loop

<?php
$img_id = get_post_thumbnail_id(35); // 35 being the ID of the Post
$img_url = wp_get_attachment_image_src($img_id);
$img_url = $img_url[0];
?>

If you also needed to get the height and width of the thumbnail, you can use the follow lines of code:

$img_url = $img_url[1]; // Get the width
$img_url = $img_url[2]; // Get the height

You can read more on the official codex about the wp_get_attachment_image_src function.

Questions, thoughts or comments, please let us know in the comments.

Leave a Reply

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