2

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

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();
|improve this answer|||||
  • Thanks passatgt. So there is no easier way to retrieve an object from the featured image? They really should implement it as a core function. – Staffan Estberg Nov 6 '14 at 10:06
  • Well, usually you only need 1 size. You can use Advanced Custom Fields with the image field which will retrieve a full array with all sizes, but if you place the function i wrote inside your functions.php, its not that complicated. – passatgt Nov 6 '14 at 10:11
  • The reason I want all sizes is because I'm using Picturefill to adhere to responsive images. WP really needs to address this within the system. Thanks for your suggestions. – Staffan Estberg Nov 6 '14 at 10:12
2

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
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.