Getting URL of Post Thumbnails in WordPress 2.9
29 Jan
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.



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
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!
Thanks for sharing.
Your tip worked perfectly.
I am also having trouble figuring out how to implement the code. I’ve tried both in the functions.php and within index.php/
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 …
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
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 …
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.
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.
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!
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/
I got it, thanks!
Would you like to share with us how you used the code?
Sure:
See here: http://www.leewillis.co.uk/patches/post-thumbnail-url/thumbnailexample.txt
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
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
Brilliant, you just helped me out massively with this
Thanks!
Many thanks for posting this. Exactly what I needed.
Matt
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.