Reperire le immagini di una gallery da un post di WordPress è un'operazione relativamente semplice.
La seguente funzione di utility restituisce un array contenente gli ID delle immagini di una gallery:
function my_get_post_gallery( $post_id ) {
$images = null;
if ( get_post_gallery( $post_id ) ) {
$gallery = get_post_gallery( $post_id, false );
$ids = $gallery['ids'];
$images = explode( ',', $ids );
}
return $images;
}
Esempio d'uso:
while( have_posts() ):
the_post();
$gallery = my_get_post_gallery( get_the_ID() );
if( !is_null( $gallery ) ) {
//...
}
endwhile;