Lee Willis

Getting URL of Post Thumbnails in WordPress 2.9

| 40 Comments

One of the improvements in WordPress 2.9 is native support for thumbnail images on posts. The API is pretty good – although it assumes that you want to return the whole IMG tag. In my case I wanted to return the URL of the image (So I could wrap the actual thumbnail in a lightbox-style link to the larger versions).

So – if you want to get the URL of your posts thumbnail – here’s the code you need:

$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];

I hope this helps you with your theme development.

40 Comments

  1. Thanks, helped me sort out my theme a bit – always defaults to the article permalink – is a little frustrating it can’t be easily set

  2. Hi, I am trying to get the post-thumbnail to link to its orginal size, and even with your help above, I have no clue how I should put this in my theme’s index.php. Could you please tell me where the above mentioned code should go (functions.php?) and what is the code to put in index.php to make it all show?

    Sorry…I *really* have no clue, I’d appreciate your help!

  3. Thanks for sharing.
    Your tip worked perfectly.

  4. I am also having trouble figuring out how to implement the code. I’ve tried both in the functions.php and within index.php/

  5. Hi taxbax,

    What problems are you having exactly? The normal usage for this would be to use it within your theme files wherever you wanted to use the URL. That may be in home.php, single.php, or wherever really.

    It does assume that you’re inside a WordPress Loop though …

  6. Hi Lee, thanks for your quick response.
    I put it inside a loop, wrapped in <img src="”> right where i wanted the url to show and it didn’t return anything.

    In my opinion this is the biggest thing left out of the post thumbnail function. I am sure it will be remedied in 3.0

  7. Hmm – weird. Can you send me the file you put it in (http://www.leewillis.co.uk/contact/) and I’ll take a look when I get chance …

  8. Thank’s man. Be blessed. I was happy with the new thumbnail feature but I also needed just the URL of the file. This helped a lot.

  9. this works pritty good for me

    $image_id = get_post_thumbnail_id();
    $image_url = wp_get_attachment_image_src($image_id,’large’, true);
    echo $image_url[0];

    • That code is slightly different for two reasons:
      1. You’re asking WP to return a media icon – rather than the actual image (The third parameter in the call to wp_get_attachment_image_src = TRUE)
      2. You’re outputting the URL with echo – rather than just assigning it to a variable.

    • Hi
      Your code with miner changing is very good
      echo $image_url[0];

    • Thanks both the author and Devdatt. I was looking for a way to retrieve the thumb alone from outside the loop and his solution worked. 🙂

  10. Working perfectly for me in header.php, WP 2.9.2. Exactly what I was trying to figure out how to do, thanks so much for sharing it!

  11. Hi Lee.

    I’m a bit of a hack and this question is evidence for that 🙂

    Would you be able to show me the code I would need to paste into my theme files to display the url… I’m pretty sure that the above needs to be wrapped in something.. but what…

    Any code I will be able to drop straight into my theme to return the url? (or url in an image tag?)

    Appreciated. It’s for this site, BTW:http://threewhiterooms.com.au/category/paintings/

  12. This helped me out a lot!

    I wanted to use the thumbnail in the same manner as you did. I want to display the full size instead, and hey, what do you know.. its not documented, but using “full-size” as size works fine 🙂

  13. Brilliant, you just helped me out massively with this 🙂 Thanks!

  14. Many thanks for posting this. Exactly what I needed.

    Matt

  15. Thank you so much for posting this!! I spent so much time trying to figure this out before finding your post. Your a life saver! Also, big thanks to A. Adams and Lee for the code samples.

  16. Thank you very much for this nice to the point post, just what I was looking for.

  17. Thanks for the tip Lee, this is exactly what I was looking for. Works like a charm.

    Adam

  18. This worked great when in a single page!

    But there is a way to make it work outside the loop?
    With this: ”

    echo get_the_post_thumbnail( 1 );

    ” I can print the thumbnail image with the tags of the ID 1 post/page.
    But how to print only the URL outside the loop?

    Thanks a lot!

  19. I Found ou on this url: http://wordpress.org/support/topic/getting-post-thumbnail-url
    The solution posted by “trepmal” member is the following:

    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    <img src="” longdesc=”URL_2″ alt=”Text_2″ />

  20. Pingback: Facebook Thumbnails: Using A Woo Themes Custom Image or Wordpress Post Thumbnail | The Wordpress Warrior

  21. Thank you !! help me a lot, need to link a jpg to the thumbnail and work perfect !

  22. Hi , thank you very much. But i need the full size picture in the post but i cant get it. Do you know what i shoud do? I have got this
    $image_id = get_post_thumbnail_id($post);
    $image_url = wp_get_attachment_image_src($image_id,’full-size’);
    $image_url = $image_url[0];

  23. Thank you, thank you, thank you!!! I’ve been searching for this for hours on the WordPress Codex and forums! I knew it shouldn’t be difficult to get just the URL of the attachment instead of it showing up as a complete img tag. I didn’t think it would be so hard to find the answer.

  24. Pingback: Getting the WordPress Post Thumbnail URL | Mark Wilkinson on WordPress

  25. Took me a minute to figure it out but it works perfectly, THANK YOU!

  26. 10x a lot man.. helped me activate light box 🙂

  27. Thanks a million, this was just what I needed.

  28. Thank you! This is fantastic. Works like a charm!

  29. Exactly what I was looking for. Works perfect for me, thank you!

  30. Hi Lee,

    Thanks so much for this bit of code, it helped tremendously.

    I was just curious though. I’m incorporating it into a CSS sheet that has several IDs with background images. However, it is using the same image for each of the specific IDs. Is there a way to to make the image url link to the thumbnail of its particular post without repeating?

    Thanks!

Leave a Reply

Required fields are marked *.