How To Get Post Thumbnail URL In WordPress

WordPress has had post thumbnail feature since version 2.9. But untill now, there isn’t offical function to get post thumbnail url. The get_the_post_thumbnail() function is very close, but it only returns HTML code. Of course we can parse this code using regular expression to get thumbnail url. But we have another better solution as the following:

if (has_post_thumbnail()) {
    $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail_name');
    echo $thumb[0]; // thumbnail url

There’s one thing that I think useful: the post thumbnail actually is one attached image to post. Therefore, we can use wp_get_attachment_image_src() to get its url.


  1. hi,
    your code works for me…

    thank you!

  2. Thank you! Worked fine!

  3. how to add hyperlink to featured images and the hyper link would b the alt tag itself??

  4. oh well, i don’t know why it didn’t work, if anyone has any hints or knows the answer please let me know. I went another route and used the_post_thumbnail to pit in an img element and with css i made it work. Thanks for your help!

    • @David D : I had the need for something similar a few days ago, and I also wrote some notes on it. You can read it here, and I am sure it will be of some help to you.

  5. again it was stripped but it would be this: echo $imgurl

    • ok so i placed that code in the loop to test and nothing showed between the to pre tags, so i guess that’s telling me that a featured image isn’t attached to the post?

      and you meant the HTML tags in the code when i present them here or in my loop? Because if it’s the latter, I don’t understand why?

      Thanks for your help

      • Tran Ngoc Tuan Anh

        April 26, 2012 at 3:52 PM

        Nothing shown means there’s no image. Just check the post in the admin area again to make sure there’s a featured image.

        Regarding the HTML tags, I meant the comment form, not your code.

        • so i did that, and it’s there, so i’m still at a loss. could it be that i added a custom size through add_image_size and for some reason it’s not showing up? i checked the uploads folder and it’s in there so i’m not sure what’s happening here

  6. it doesn’t work for me and i’m not sure why. Nothing gets populated. How can I show my code?

    • Tran Ngoc Tuan Anh

      April 26, 2012 at 2:01 AM

      You can show your code here by wrapping it in PRE tag. Or you can paste your code on and give me a link.

      • rewind_posts();
            global $wp_query, $paged;
            $og_query = $wp_query;
            $wp_query = null;
            $wp_query = new WP_Query('posts_per_page=3&paged='.$paged);
            $image_id = get_post_thumbnail_id();
            $ssimg = wp_get_attachment_image_src($image_id,'slideshow-bk');
            $imgurl = $ssimg[0];
        if (have_posts()) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
            <div class="slide" style="background-image: url(''); width:940px; height:400px;">              
                              <a href="" class="" rel="bookmark" title="Permanent Link to ">
        <?php endwhile; else:
              endif; $wp_query = null; $wp_query = $og_query; wp_reset_postdata();
        • not sure while it was stripped out, but inside url(‘ ‘) should be

        • Tran Ngoc Tuan Anh

          April 26, 2012 at 6:52 AM

          Can you add this to confirm that there’s a featured image for the post:

          echo '<pre>
          print_r( $ssimg );
          echo '</pre>';

          PS: All HTML tags in the code should be translated to their entities to display correctly.

  7. Works, Brilliant.


  8. Thanks very much. I’ll use it for video sitemap.

Leave a Reply

Your email address will not be published.