The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Implementing an SVG Icon System" Nov 30 - 6:00 PM Eastern

Get Featured Image URL

Last updated on:

Post thumbnails are pretty useful and pretty easy to use in WordPress. Simply add:


To a theme's functions.php file and you'll get a Featured Image module on the admin screen for posts which allows you to select one.

It is also very easy to output that image as an HTML <img>:


But what if you just need the URL? Say, you're going to use it as a background-image on an element rather than a content image. Unfortunately there is no super easy/obvious function for that.

Within the loop, you'll have to do:

$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];

Then $thumb_url will be that URL.


  1. Aaron
    Permalink to comment#

    You can also use this code, especially when storing posts in an array:

    $image = wp_get_attachment_url( get_post_thumbnail_id($post->ID));

    • Gerson
      Permalink to comment#

      Saved my butt. Thanks man.

      I am new both WordPress and php so I purchased a theme for my site to simplify things. I have been making changes to the site and was struggling for hours on getting the post attachment url. My problem was that I was looking through the theme functions and files. Oh well. you live and you learn. Thanks again.

    • Ben Dunkle
      Permalink to comment#

      Aaron, how would you pass the thumbnail size in your code? I tried it, and it shows the url of the original image.

    • Jose Vega
      Permalink to comment#

      Hi Ben, in that case you can use this:

      wp_get_attachment_image_src( get_post_thumbnail_id($post-&gt;ID), $size, $icon );

      You can set a registered size on the second parameter…

  2. Native Imaging

    Hey Chris, i’m a little stumped on a task with trying to get the featured image source of the next and previous post. i know how to get the featured image of the next and previous posts, im using them for pretty Next/Prev nav buttons, but im also using featured video plus plugin in my theme, so it auto replaces the image with the featured video. excellent plugin, and ive got just the video thumbnail to work on my blog and cat pages by grabbing the img src rather than post_thumbnail, but still stumped on how to do this for Next/Prev navigation on single posts. Any tips you can point out for me?

    Thanx! :)

  3. Igor
    Permalink to comment#

    Hi. I am trying to achieve this in one go. But I am not very familiar with PHP.

    I would like to print the complete style with PHP and fetch featured image. Tried something like this inside a simple WP plugin I’v made:

    Print '<style> #slider_dyn_img_layer { background-image: url("' 
           , wp_get_attachment_image_src( get_post_thumbnail_id($ID) ) , 
           '") !important ; } </style>';

    But it didn’t work and when debugging without the WP commands I’v also noticed that the script printed its result at the first line in the document. Preferably I would like the output to show up inside .

    Any help would be very appreciated!

  4. Edward Beckett
    Permalink to comment#

    One Liner ~ <?= wp_get_attachment_image_src( get_post_thumbnail_id(), 'full', false )[0] ?>

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed