Obtener la imagen destacada como objeto en WordPress
Quiero recuperar la imagen destacada de una entrada como un objeto (array
) para tener disponibles todos los tamaños de imagen.
La función get_the_post_thumbnail()
no hace esto, ¿alguna idea?

Primero obtén los tamaños de imagen registrados y el ID del adjunto de la imagen destacada:
$sizes = get_intermediate_image_sizes();
$post_thumbnail_id = get_post_thumbnail_id();
Recorre los tamaños registrados y crea un array:
$images = array();
foreach ( $sizes as $size ) {
$images[] = wp_get_attachment_image_src( $post_thumbnail_id, $size );
}
Combinado como una función para colocar dentro de 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;
}
Uso:
$featured_image_sizes = get_all_image_sizes();

Gracias passatgt. Entonces, ¿no hay una forma más fácil de recuperar un objeto de la imagen destacada? Realmente deberían implementarlo como una función nativa.

Bueno, normalmente solo necesitas 1 tamaño. Puedes usar Advanced Custom Fields con el campo de imagen que recuperará un array completo con todos los tamaños, pero si colocas la función que escribí dentro de tu functions.php, no es tan complicado.

Esto es antiguo, pero la respuesta anterior no es del todo completa. Para obtener correctamente todos los tamaños de imagen con todos los atributos de la imagen, también necesitarías obtener el objeto de adjunto.
Algo como esto:
if ( has_post_thumbnail() ) {
$thumb = array();
$thumb_id = get_post_thumbnail_id();
// primero obtenemos toda la información sobre la imagen... título/descripción/alt/etc.
$args = array(
'post_type' => 'attachment',
'include' => $thumb_id
);
$thumbs = get_posts( $args );
if ( $thumbs ) {
// ahora creamos el nuevo 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 )
);
// añadimos los tamaños adicionales de imagen
foreach ( get_intermediate_image_sizes() as $size ) {
$thumb['sizes'][$size] = wp_get_attachment_image_src( $thumb_id, $size, false );
}
} // end if
// mostramos la imagen con el tamaño 'custom-size'
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, otra actualización después de algunos años. Supongo que ya lo habrás conseguido ;). Pero para aquellos que quieran hacer esto y devolver algo que sea consistente con - digamos - objetos de imagen ACF y que permita rellenar fácilmente los sourcesets. Podrías hacer algo como esto en 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;
}
Y luego puedes usarlo así
$thumbID = get_post_thumbnail_id();
$image = get_all_image_sizes($thumbID);
$html = '<img src="'. $image['sizes']['large'] .'" alt="" title="">';
