How to get ID of images used in gallery?

How to get ID of images used in gallery?
Using $gallery = get_post_gallery_images( $post ); I only get a string with thumbnails url, without image ID which is needed for basically everything.

https://codex.wordpress.org/Function_Reference/get_post_gallery_images

(In the end I am looking for a way to get image object for all images used in gallery, I am searching in Codex but I can’t find it)

1 Answer
1

Are you writing a template? A filter in functions.php or a plugin? A straightforward method could be using get_post_gallery with the second argument set to false, so that it return the object rather than the html.

if ( get_post_gallery() ) :

    //Get the gallery object
    $gallery = get_post_gallery( get_the_ID(), false );

    //Form an array with the found ids
    $gallery_attachment_ids = explode( ',', $gallery['ids'] );

endif;

Leave a Comment