Find the src of a particlular size of the WordPress post thumbnail

I used to add Featured Images to my wordpress templates using:

<?php the_post_thumbnail( 'large' ); ?>

However as I now often need to use srcset to provide a list of possible images for the browser to choose from I need to find the urls of various sizes of the Featured Image. For this I use:

<?php wp_get_attachment_image_src( $attachment_id, $size, $icon ); ?>


(integer) (required) ID of the desired attachment.
Default: None

(string/array) (optional) Size of the image shown for an image attachment: either a string keyword (thumbnail, medium, large, or full), or a 2-item array representing width and height in pixels, e.g., array(32,32).
Default: thumbnail

(bool) (optional) Use a media icon to represent the attachment.
Default: false

So to get the url of the large version of my Featured Image I use this:

$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large', false, '' ); 
echo $src[0]; 

More information can be found here: https://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s