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)

One thought on “Getting the post thumbnail URL”

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