WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I see many methods of showing thumbnails in WordPress, but I'm not immediately sure how I could get only the path to a post's thumbnail rather than the html-ready code generated by functions like the_post_thumbnail() and get_the_post_thumbnail().

What methods are available to me to get only the path of the thumbnail (to set it as a bgimage) rather than the <img /> tag? Do I only have the option of parsing the results of the get_ method or is there an easier way?

share|improve this question
Are your images part of a post gallery or just included in the media library? – PNMG Dec 1 '10 at 21:19
All are attached to posts - they are thumbnails to posts. – Sampson Dec 1 '10 at 21:35
up vote 6 down vote accepted

Thumbnail is essentially attachment so you can approach from that side - lookup ID with get_post_thumbnail_id() and fetch data with wp_get_attachment_image_src(), like this:

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


share|improve this answer

One method would be to convert whatever is returned from get_the_post_thumbnail() to an object, and pull the src attribute:

$thumbnail = new SimpleXMLElement( get_the_post_thumbnail( $postid ) );
print $thumbnail->attributes()->src;
share|improve this answer

When I need to display a thumbnail that is attached to a post gallery, I use a custom function in my functions.php. It might be over kill for your needs, but it should cover everything.

In this example, I retrieve all the images in a post's gallery, and then display each image in a list item. The list contains the thumbnail image wrapped in an anchor that links to the post the image came from. The output string can easily be customized to your needs.

function get_gallery_image_thumb_list($size){
global $post;
$args = array(
    'numberposts' => null,
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'nopaging' => false,
    'post_mime_type' => 'image',
    'order' => 'ASC',
    'orderby' => 'menu_order ID',
    'post_status' => 'any'

$attachments =& get_children($args);
if ($attachments) {
    foreach($attachments as $attachment) {
        foreach($attachment as $attachment_key => $attachment_value) {

            $imageID = $attachment->ID;
            $imageTitle = $attachment->post_title;
            $imageCaption = $attachment->post_excerpt;
            $imageDescription = $attachment->post_content;
            $imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true);
            $imageArray = wp_get_attachment_image_src($attachment_value, $size, false);
            $imageURI = $imageArray[0]; // 0 is the URI
            $imageWidth = $imageArray[1]; // 1 is the width
            $imageHeight = $imageArray[2]; // 2 is the height

            // Build the <img> string
            $ImgString = '<li><a href="' . get_permalink() . '" title="' . the_title("", "", false) . '"><img src="' . $imageURI . '" width="' . $imageWidth . '" height="' . $imageHeight . '" alt="' . $imageAlt . '" title="' . $imageTitle . '" /></a></li>';
            // Print the image
            echo $ImgString;


Then call the function and pass in the size of image you want returned (thumbnail, medium, large or full) like so:


This will need to be called in The Loop or a custom loop.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.