post thumbnails - Retrieve featured image as object

I want to retrieve the featured image of a post as an object (array) in order to have all image sizes available.The get

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742267303a4412044.html

相关推荐

  • post thumbnails - Retrieve featured image as object

    I want to retrieve the featured image of a post as an object (array) in order to have all image sizes available.The get

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信