The get_post_galleries_images() WordPress PHP function retrieves the image srcs from galleries in a post’s content, if present.
Usage
To use the get_post_galleries_images() function, simply pass in the $post
parameter, which can be a Post ID or a WP_Post object:
$galleries = get_post_galleries_images( $post );
Parameters
$post
(int|WP_Post) – Optional. Post ID or WP_Post object. Default is the global$post
.
More information
See WordPress Developer Resources: get_post_galleries_images()
Examples
Displaying Gallery Image URLs in a List
This example adds a list of image URLs to the content of any post or page that has at least one gallery:
function display_gallery_image_urls( $content ) { global $post; // Only do this on singular items if ( ! is_singular() ) { return $content; } // Make sure the post has a gallery in it if ( ! has_shortcode( $post->post_content, 'gallery' ) ) { return $content; } // Retrieve all galleries of this post $galleries = get_post_galleries_images( $post ); if ( ! empty( $galleries ) ) { $image_list = '<ul>'; // Loop through all galleries found foreach( $galleries as $gallery ) { // Loop through each image in each gallery foreach ( $gallery as $image ) { $image_list .= '<li>' . $image . '</li>'; } } $image_list .= '</ul>'; // Append our image list to the content of our post $content .= $image_list; } return $content; } add_filter( 'the_content', 'display_gallery_image_urls' );
Output All Image URLs from a Specific Post
This example outputs all image URLs from a specific post with a given post ID:
function output_gallery_image_urls( $post_id ) { // Get the post object $post = get_post( $post_id ); // Retrieve all galleries of this post $galleries = get_post_galleries_images( $post ); // Output the image URLs foreach ( $galleries as $gallery ) { foreach ( $gallery as $image ) { echo $image . '<br>'; } } } output_gallery_image_urls( 123 ); // Replace 123 with the desired post ID