Reputation: 882
I want to get all of the sizes of a post's featured image as an array so that I can easily use them in the srcset
attribute.
Currently, I am using the following code to get a post's featured image in a single size so that it can be used as a background image for a teaser;
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'banner' )[0]
<img src="<?php echo $image; ?>">
This obviously only returns the URL of the "banner" size.
I have 4 sizes, "banner"
, "large"
, "medium"
, and "thumbnail"
, that I want in an array so I can easily access all the sizes without having to repeat the above code 4 times.
e.g.
$image[0]; // banner
$image[1]; // large
$image[2]; // medium
$image[3]; // thumbnail
Is there an easy built-in way to do this? And if not a function that does this would be appreciated!
Upvotes: 3
Views: 1271
Reputation: 9373
There is no direct method to get all the size of the featured image in WordPress. For that you to do it manually.
First, you need to get the array of registered image sizes.
$image_sizes = get_intermediate_image_sizes();
Then Loop through the image sizes, and use them to output the Featured Image.
foreach ( $image_sizes as $image_size ) {
echo '<li>';
the_post_thumbnail( $post->ID, $image_size );
echo '</li>';
}
Upvotes: 3