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.

Share This:
  • Digg
  • del.icio.us
  • Facebook
  • Technorati
  • Twitter

20 Responses to “Getting URL of Post Thumbnails in WordPress 2.9”

  1. cube 16. Feb, 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

  2. Fieke 03. Mar, 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!

  3. Fabiano 28. Mar, 2010 at 8:10 pm #

    Thanks for sharing.
    Your tip worked perfectly.

  4. taxbax 29. Mar, 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/

  5. Lee 29. Mar, 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 …

  6. taxbax 29. Mar, 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

  7. Lee 29. Mar, 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 …

  8. Vlad 23. Apr, 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.

  9. Devdatt Gurjar 26. Apr, 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];

    • Lee 26. Apr, 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.

  10. KYJ 17. May, 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!

  11. Ali Starbright 09. Jun, 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/

  12. Jimmy Rosén 11. Jun, 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 :)

  13. mynameisorman 18. Jun, 2010 at 9:44 am #

    Brilliant, you just helped me out massively with this :) Thanks!

  14. Matt 23. Jul, 2010 at 8:34 pm #

    Many thanks for posting this. Exactly what I needed.

    Matt

  15. Patrick Hill 31. Jul, 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.

Leave a Reply