I want to retrieve the featured image of a post as an object (array
) in order to have all image sizes available.
The get_the_post_thumbnail()
function doesn’t do this, any ideas?
I want to retrieve the featured image of a post as an object (array
) in order to have all image sizes available.
The get_the_post_thumbnail()
function doesn’t do this, any ideas?
First get the registered image sizes and the featured image attachment id:
$sizes = get_intermediate_image_sizes();
$post_thumbnail_id = get_post_thumbnail_id();
Loop through the registered sizes and create an array:
$images = array();
foreach ( $sizes as $size ) {
$images[] = wp_get_attachment_image_src( $post_thumbnail_id, $size );
}
Combined as a function to place inside functions.php:
function get_all_image_sizes($attachment_id = 0) {
$sizes = get_intermediate_image_sizes();
if(!$attachment_id) $attachment_id = get_post_thumbnail_id();
$images = array();
foreach ( $sizes as $size ) {
$images[] = wp_get_attachment_image_src( $attachment_id, $size );
}
return $images;
}
Usage:
$featured_image_sizes = get_all_image_sizes();