Come ottenere l'immagine in evidenza come oggetto in WordPress

6 nov 2014, 09:38:44
Visualizzazioni: 15.1K
Voti: 5

Voglio recuperare l'immagine in evidenza di un post come oggetto (array) per avere a disposizione tutte le dimensioni dell'immagine.

La funzione get_the_post_thumbnail() non fa questo, avete idee?

0
Tutte le risposte alla domanda 3
3

Per prima cosa ottieni le dimensioni delle immagini registrate e l'ID dell'allegato dell'immagine in evidenza:

$sizes = get_intermediate_image_sizes();
$post_thumbnail_id = get_post_thumbnail_id();

Cicla attraverso le dimensioni registrate e crea un array:

$images = array();
foreach ( $sizes as $size ) {
    $images[] = wp_get_attachment_image_src( $post_thumbnail_id, $size );
}

Combinato in una funzione da inserire nel file 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;
}

Utilizzo:

$featured_image_sizes = get_all_image_sizes();
6 nov 2014 09:59:26
Commenti

Grazie passatgt. Quindi non c'è un modo più semplice per recuperare un oggetto dall'immagine in evidenza? Dovrebbero davvero implementarlo come una funzione core.

Staffan Estberg Staffan Estberg
6 nov 2014 10:06:16

Beh, di solito hai bisogno solo di una dimensione. Puoi usare Advanced Custom Fields con il campo immagine che recupererà un array completo con tutte le dimensioni, ma se inserisci la funzione che ho scritto nel tuo functions.php, non è così complicato.

passatgt passatgt
6 nov 2014 10:11:02

Il motivo per cui voglio tutte le dimensioni è perché sto usando Picturefill per aderire alle immagini responsive. WP dovrebbe davvero affrontare questo all'interno del sistema. Grazie per i tuoi suggerimenti.

Staffan Estberg Staffan Estberg
6 nov 2014 10:12:58
0

Questo è vecchio, ma la risposta sopra non è del tutto completa. Per ottenere correttamente tutte le dimensioni delle immagini con tutti gli attributi dell'immagine, dovresti anche recuperare l'oggetto attachment.

Qualcosa come questo:

if ( has_post_thumbnail() ) {
    $thumb = array();
    $thumb_id = get_post_thumbnail_id();

    // prima recupera tutte le informazioni sull'immagine... titolo/descrizione/alt/ecc.
    $args = array(
        'post_type' => 'attachment',
        'include' => $thumb_id
    );
    $thumbs = get_posts( $args );
    if ( $thumbs ) {
        // ora crea il nuovo 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 )
        );
        // aggiungi le dimensioni aggiuntive dell'immagine
        foreach ( get_intermediate_image_sizes() as $size ) {
            $thumb['sizes'][$size] = wp_get_attachment_image_src( $thumb_id, $size, false );
        }
    } // end if

    // mostra l'immagine con dimensione personalizzata
    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
30 set 2016 21:05:37
0

Ok, un altro aggiornamento dopo alcuni anni. Presumo che ormai tu abbia risolto ;). Ma per coloro che vorrebbero farlo e ottenere qualcosa di coerente con - diciamo - gli oggetti immagine ACF e che permetta di popolare facilmente i sourceset. Potresti fare qualcosa del genere 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;
} 

E poi puoi usarla così

   $thumbID = get_post_thumbnail_id();
   $image = get_all_image_sizes($thumbID);
   $html = '<img src="'. $image['sizes']['large'] .'" alt="">';
13 lug 2020 09:41:17