The get_post_gallery_images() WordPress PHP function checks a post’s content for galleries and returns the image srcs for the first found gallery.
Usage
get_post_gallery_images( $post );
Parameters
$post
(int|WP_Post) – Optional. Post ID or WP_Post object. Default is global$post
.
More information
See WordPress Developer Resources: get_post_gallery_images
Examples
Display Gallery Image URLs Below the Content
This example appends the raw image URLs to the content of any post or page that has at least one gallery.
add_filter( 'the_content', 'wpdocs_show_gallery_image_urls' ); function wpdocs_show_gallery_image_urls( $content ) { global $post; if( ! is_singular() ) return $content; if( ! has_shortcode( $post->post_content, 'gallery' ) ) return $content; $gallery = get_post_gallery_images( $post ); $image_list = '<ul>'; foreach( $gallery as $image_url ) { $image_list .= '<li>' . '<img src="' . $image_url . '">' . '</li>'; } $image_list .= '</ul>'; $content .= $image_list; return $content; }
Note: This does not work if the gallery type is set to ‘slideshow’. It returns nothing. Removing type="slideshow"
from the gallery shortcode allows the images to be picked up by this function.