Updated: November 1, 2017
Find the src of a particlular size of the WordPress post thumbnail
I used to add Featured Images to my wordpress templates using:
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:
wp_get_attachment_image_src( $attachment_id, $size, $icon );
(integer) (required) ID of the desired attachment.
(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).
(bool) (optional) Use a media icon to represent the attachment.
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;
More information can be found here: https://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src