DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Today, here’s a snippet that gives you the ability to extract the source of the post thumbnail image. It is super useful if you need the post thumbnail url to use in your code.

Simply paste the following code on your theme file, within the loop.

<?php
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id);
$image_url = $image_url[0];
?>

Thanks to WP Snippets for the cool tip!

Tags

13 Comments

  1. You could simplify and make this a little more efficient in this way:

    Less variable use and fewer lines.

  2. Gah. the previous comment stripped my code. May need to edit to make it show properly.

  3. Great, its simple short and sweet, earlier i used a lengthy code to achieve the same result

  4. How about wp_get_attachment_thumb_url()? 🙂

  5. Don’t forget about the width and the height the function you used returns, it’s always a good idea to include the attributes if you’re displaying your image on screen. Also, always check for has_post_thumbnail first:

    if ( has_post_thumbnail() ) {
    $image_id = get_post_thumbnail_id();
    list( $src, $width, $height ) = wp_get_attachment_image_src($image_id);
    echo “”;
    }

    If you’re paranoid you can esc_url and absint as well 🙂

    Cheers!

    ~ K

  6. You should also get <pre> tags working 🙂

  7. Very nice!

    I tried to understand wordpress theme better.
    And I found this site.

    very useful .
    Thanks !!

  8. this site is very helpful to me…thanks guys…:)

  9. This snippet is very useful….thanks 🙂

  10. i tried the wp_get_attachment_thumb_url() but its not working, im using elegant themes.. pls help.. my account on ET already expired..

  11. Can you please simplify this for non techie wordpress newbies? I don’t even know what you mean by “the loop”? I just want to add an image to my side bar, was asked for the image URL and I can’t find it. Can you please, in simple terms, explain how to find the image URL for an image that you have downloaded onto your computer and want to insert into your website? Thanks.

  12. I usually use the way like Kontantin comment

  13. hi.
    how i can get post thumbnail url, i named ‘slider’?

    thanks

Leave a Comment

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