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?
3 Answers
Reset to default 6First 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();
This is old, but the above answer isn't quite complete. To properly get all image sizes with all of the image attributes, you'd need to grab the attachment object as well.
Something like this:
if ( has_post_thumbnail() ) {
$thumb = array();
$thumb_id = get_post_thumbnail_id();
// first grab all of the info on the image... title/description/alt/etc.
$args = array(
'post_type' => 'attachment',
'include' => $thumb_id
);
$thumbs = get_posts( $args );
if ( $thumbs ) {
// now create the new array
$thumb['title'] = $thumbs[0]->post_title;
$thumb['description'] = $thumbs[0]->post_content;
$thumb['caption'] = $thumbs[0]->post_excerpt;
$thumb['alt'] = get_post_meta( $thumb_id, '_wp_attachment_image_alt', true );
$thumb['sizes'] = array(
'full' => wp_get_attachment_image_src( $thumb_id, 'full', false )
);
// add the additional image sizes
foreach ( get_intermediate_image_sizes() as $size ) {
$thumb['sizes'][$size] = wp_get_attachment_image_src( $thumb_id, $size, false );
}
} // end if
// display the 'custom-size' image
echo '<img src="' . $thumb['sizes']['custom-size'][0] . '" alt="' . $thumb['alt'] . '" title="' . $thumb['title'] . '" width="' . $thumb['sizes']['custom-size'][1] . '" height="' . $thumb['sizes']['custom-size'][2] . '" />';
} // end if
Ok another update to this after some years. I presume that you managed by now ;). But for those who would like to do this and return something that is consistent with - let's say - ACF image objects and allows you to easily populate sourcesets. You could do something like this in 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[$size] = wp_get_attachment_image_src( $attachment_id, $size )[0];
}
$imageObject = array(
'sizes' => $images
);
return $imageObject;
}
And then you can use it like this
$thumbID = get_post_thumbnail_id();
$image = get_all_image_sizes($thumbID);
$html = '<img src="'. $image['sizes']['large'] .'" alt="">';