WordPress: visualizzare tutte le immagini associate ad un post

Ad un post di WordPress possono essere associate svariate immagini. Ma come possiamo reperire queste immagini e visualizzarle a parte? Vediamo insieme la soluzione.

Possiamo utilizzare la funzione get_children() specificando come tipo di post attachment e come MIME Type image:


<?php
$attachments = get_children(array('post_parent' => $post->ID,
		'post_status' => 'inherit',
		'post_type' => 'attachment',
		'post_mime_type' => 'image',
		'order' => 'ASC',
		'orderby' => 'menu_order ID'));

foreach($attachments as $att_id => $attachment) {
	$full_img_url = wp_get_attachment_url($attachment->ID);
?>

	<img src="<?php echo $full_img_url;?>"/>
<?php
}
?>

Ovviamente questa soluzione si applica anche agli altri tipi di allegati di un post (รจ sufficiente cambiare il MIME Type).

Torna su