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.
February 16, 2010 at 11:55 pm
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
March 3, 2010 at 5:41 pm
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!
March 28, 2010 at 8:10 pm
Thanks for sharing.
Your tip worked perfectly.
March 29, 2010 at 6:34 pm
I am also having trouble figuring out how to implement the code. I’ve tried both in the functions.php and within index.php/
March 29, 2010 at 7:55 pm
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 …
August 9, 2011 at 5:58 pm
What do I do with this code bit of code? None of that was specified.
March 29, 2010 at 7:58 pm
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
March 29, 2010 at 8:01 pm
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 …
April 23, 2010 at 7:57 am
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.
April 26, 2010 at 1:42 am
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];
April 26, 2010 at 7:29 am
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.
October 26, 2010 at 9:40 am
Hi
Your code with miner changing is very good
echo $image_url[0];
July 11, 2011 at 6:54 pm
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. 🙂
May 17, 2010 at 7:35 am
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!
June 9, 2010 at 10:20 am
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/
June 9, 2010 at 10:28 am
I got it, thanks!
June 22, 2010 at 10:51 pm
Would you like to share with us how you used the code?
June 22, 2010 at 10:56 pm
Sure:
See here: http://www.leewillis.co.uk/patches/post-thumbnail-url/thumbnailexample.txt
June 22, 2010 at 11:17 pm
Thanks! There were a couple of errors in the example URL but I fixed them, so now it works: http://cloudislands.com/samples/snip.txt
June 11, 2010 at 11:10 am
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 🙂
June 18, 2010 at 9:44 am
Brilliant, you just helped me out massively with this 🙂 Thanks!
July 23, 2010 at 8:34 pm
Many thanks for posting this. Exactly what I needed.
Matt
July 31, 2010 at 4:50 pm
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.
September 4, 2010 at 6:07 pm
Thank you very much for this nice to the point post, just what I was looking for.
January 21, 2011 at 10:31 am
Thanks for the tip Lee, this is exactly what I was looking for. Works like a charm.
Adam
February 18, 2011 at 8:23 pm
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!
February 18, 2011 at 8:50 pm
Just pass the post ID to get_post_thumbnail_id(), e.g. get_post_thumbnail_id(1); then the rest of the code stays as is.
February 18, 2011 at 8:28 pm
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″ />
Pingback: Facebook Thumbnails: Using A Woo Themes Custom Image or Wordpress Post Thumbnail | The Wordpress Warrior
May 13, 2011 at 3:10 pm
Thank you !! help me a lot, need to link a jpg to the thumbnail and work perfect !
June 29, 2011 at 6:05 pm
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];
June 29, 2011 at 8:34 pm
The standard image sizes are thumbnail, medium or large.
Putting anything else should return an image that is “content_width” sized – or 500px if that’s not set – see http://mainstreetopen.com/wordpress-26-and-full-width-images/
July 3, 2011 at 9:09 pm
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.
Pingback: Getting the WordPress Post Thumbnail URL | Mark Wilkinson on WordPress
August 18, 2011 at 4:57 pm
Took me a minute to figure it out but it works perfectly, THANK YOU!
August 24, 2011 at 2:24 am
10x a lot man.. helped me activate light box 🙂
September 13, 2011 at 3:39 pm
Thanks a million, this was just what I needed.
December 8, 2011 at 10:28 pm
Thank you! This is fantastic. Works like a charm!
January 18, 2012 at 9:22 am
Exactly what I was looking for. Works perfect for me, thank you!
July 24, 2012 at 6:50 pm
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!