4

I am using <?php wp_get_attachment_thumb_url( $attachment_id ); ?> to get the url of a thumbnail for a post in Wordpress. However I would like to retrieve the URL of a specific size image. So for example when displaying the post thunmbnail normally you can specify a size, like: <?php the_post_thumbnail( $size, $attr ); ?>. Is it possible to do the same with wp_get_attachment_thumb_url.

Something like: <?php wp_get_attachment_thumb_url( $attachment_id, $size ); ?>?

2 Answers 2

1

You want wp_get_attachment_image_src():

if ( $src = wp_get_attachment_image_src( $attachment_id, $size ) ) {
    echo $src[0]; // URL
    echo $src[1]; // Width
    echo $src[2]; // Height
}
1

Here is how to retrieve the URL for a different size of the featured image (post thumbnail). In this example I’m echoing out the “large” size:

<?php echo wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large')[0]; ?>

Works both inside and outside of the loop. (I use this to fill out meta tags in the head for itemprop, Open Graph, etc.)

The [0] at the end is necessary because wp_get_attachment_image_src returns an array, with the first value being the URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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