WordPress `get_the_post_thumbnail` Source URL

This will return a fully formatted HTML <img ... > tag.

$thumb_id = $post->ID;

$pictureLink = get_the_post_thumbnail($thumb_id, 'medium');

What function can I use to get the ‘src’ tag value?

Read More

I tried some other WP functions for attachments but these did not return anything useful for some reason.

I will accept a WP function or a parser that will extract the src tag’s value.

Update:
Parser that will extract the src tag’s value.

$doc = new DOMDocument();
$doc->loadHTML($pictureLinkHtml);
$xpath = new DOMXPath($doc);
$pictureLink = $xpath->evaluate("string(//img/@src)");

Related posts

2 comments

  1. You can do this a lot easier than parsing it. First you need to get the attachment ID from the post ID. then you can grab the source which includes the URL, width and height. Here is more information.

    $image_id = get_post_thumbnail_id($post->ID);
    $image = wp_get_attachment_image_src($image_id, 'large');
    
    echo $image[0]; 
    

Comments are closed.