Prelevare la thumb di un’immagine, WordPress
In wordpress qualsiasi post può essere definito con una serie di elementi(attachments) come i video e le immagini . Quando inseriamo un’ immagine nel contenuto di un post è possibile prelevarla con una funzione php messa a disposizione da wordpress e isolarla dal contenuto.
$images = get_children(array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_mime_type' => 'image', 'post_parent' => $id)); if(!empty($images)){ foreach($images as $image){ //$attachment = wp_get_attachment_image_src($image->ID, $size); $attachment = wp_get_attachment_image( $image->ID, $size ); } }
La funzione get_children(), che troviamo nella prima riga di questo script, è una funzione wordpress usata per prelevare dal database attachments, revisioni o sotto pagine di un post genitore .
Wp_get_attachment_image , invece, restituisce il codice html dell’immagine. Come secondo parametro è possibile inserire tre valori : thumbnail, medium, large or full .
Quandi se velessimo prelevare la thumb dell’immagine generata da wordpress basterebbe specificare nella funzione wp_get_attachment_image il valore thumbnail e il gioco è fatto