Ottenere la Descrizione dell'Immagine

6 dic 2013, 06:29:26
Visualizzazioni: 39.5K
Voti: 19

Sto cercando di dividere un post in 2 colonne. La prima colonna a sinistra conterrà le immagini all'interno del post, mentre la seconda colonna a destra conterrà the_content() (escludendo le immagini).

Al momento non ho problemi nell'estrarre tutte le immagini. Tuttavia, non riesco a ottenere la didascalia, il titolo o la descrizione dell'immagine.

Ecco il mio codice:

<?php if ( $images = get_posts(array(
        'post_parent' => $post->ID,
        'post_type' => 'attachment',
        'numberposts' => -1,
        'orderby'        => 'title',
        'order'           => 'ASC',
        'post_mime_type' => 'image',
    )))
    {
        foreach( $images as $image ) {
            $attachmenturl = wp_get_attachment_url($image->ID);
            $attachmentimage = wp_get_attachment_image_src( $image->ID, full );
            $imageDescription = apply_filters( 'the_description' , $image->post_content );
            $imageTitle = apply_filters( 'the_title' , $image->post_title );
            $i++;
            if (!empty($imageTitle)) {
                echo '<div class="client-img-item ci-'.$count++.'"><img src="' . $attachmentimage[0] . '" alt="'.$imageTitle.'"  /> <div class="CAPS client-img-caption"><span class="red arrow-lrg">»</span> '.$imageDescription.'</div></div><div class="sml-dots"></div>';
} else { echo '<img src="' . $attachmentimage[0] . '" alt="" />' ; }
        }
    } else {
        echo 'Nessuna Immagine Trovata';
    }?>
1
Commenti

A partire da WordPress 3.5.0 la funzione wp_prepare_attachment_for_js( $attachment ) farà al caso tuo :)

Sven Sven
26 mar 2018 11:59:21
Tutte le risposte alla domanda 1
1
28
function wp_get_attachment( $attachment_id ) {

$attachment = get_post( $attachment_id );
return array(
    'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), // Testo alternativo dell'immagine
    'caption' => $attachment->post_excerpt, // Didascalia
    'description' => $attachment->post_content, // Descrizione
    'href' => get_permalink( $attachment->ID ), // URL del permalink
    'src' => $attachment->guid, // URL sorgente dell'immagine
    'title' => $attachment->post_title // Titolo
);
}

Fonte

Come sporkme spiega più avanti nella discussione, questo codice va inserito nel tuo file functions.php e può essere richiamato con $attachment_meta = wp_get_attachment(id_tua_allegato);.

6 dic 2013 19:02:05
Commenti

proviene da: //https://gist.github.com/hullen/5443218 ????

menardmam menardmam
21 nov 2015 17:12:18