Getting the post thumbnail URL

You can use wp_get_attachment_image_src() to retrieve an object with the post thumbnail information.

include ('wp-blog-header.php');
$post_id = $_REQUEST['id'];
$img_id = get_post_thumbnail_id($post_id, $size = 'post-thumbnail', $attr = '' );
$image = wp_get_attachment_image_src( $img_id );

While methods such as get_the_post_thumbnail() returns an image tag (), wp_get_attachment_image_src() returns a numeric array with the thumbnail info:

  • $image[0] => url
  • $image[1] => width
  • $image[2] => height

You can also determine if a post has attachments by calling has_post_thumbnail(), which returns a Boolean (true or false)

1 comment to Getting the post thumbnail URL

  • Thanks!

    It actually looks like the only parameter available for get_post_thumbnail_id is post_id so there’s no need to specify $size or $attr there.

    However, wp_get_attachment_image_src does allow for the $size parameter.


Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

IMPORTANT! To be able to proceed, you need to solve the following simple math (so I know that you are a human) :-)

What is 7 + 13 ?
Please leave these two fields as-is: