admin管理员组

文章数量:1331538

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?

Share Improve this question edited Jul 13, 2020 at 10:07 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Nov 6, 2014 at 9:38 Staffan EstbergStaffan Estberg 3833 gold badges13 silver badges29 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

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();

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="">';

本文标签: post thumbnailsRetrieve featured image as object