Image URL with the_post_thumbnail () function

3

How can I extract only the URL (attribute value src ) from an image generated by the the_post_thumbnail function and assign it to an image?

The result I'm looking for would look something like:

<img src="<?php the_post_thumbnail(); ?>">
    
asked by anonymous 18.03.2014 / 16:32

2 answers

3

If you're in Loop , the post ID is known by WP, so just use the function get_post_thumbnail_id() without parameter. > If you're out of Loop , it's a matter of using get_post_thumbnail_id($post_id) .

And with this information at hand, use the wp_get_attachment_image_src function that indicates the Leandro .

Example:

$img = wp_get_attachment_image_src( get_post_thumbnail_id() );
echo "<img src='$img[0]' />";

How It Works : The the_post_thumbnail function is a simple encapsulation for the get_the_post_thumbnail function, which in turn ends by calling the wp_get_attachment_image function.

    
18.03.2014 / 17:20
1

You can do this as follows:

<?php
  $imagem = wp_get_attachment_image_src( $attachment_id, $size, $icon );
?>
<img src="<?php echo $imagem[0]; ?>" />

Here's the documentation if you have any questions.

    
18.03.2014 / 16:42