How To Get Post Thumbnail URL In WordPress

How To Get Post Thumbnail URL In WordPress

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. it doesn’t work for me and i’m not sure why. Nothing gets populated. How can I show my code?

    1. 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();
    2. 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.

    1. 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

    2. 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.

    3. 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

  2. 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!

    1. @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.

Comments are closed.