get_post_gallery_images()
Retrieves an array of image URLs that belong to the first gallery of the specified post.
There is a similar function get_post_galleries_images() which retrieves all galleries, not only the first one.
Gets the URLs of the thumbnails, not of the full-size images.
No Hooks.
Returns
String[]. A list of a gallery's image srcs in the order they are listed in the [gallery] shortcode.
Usage
get_post_gallery_images( $post );
- $post(int|WP_Post)
- Post ID or WP_Post object.
Default: global$post
Examples
#1 Get a list of all images SRC in the first gallery of post 2179
$gal = get_post_gallery_images(2179); /* $gal will contain Array ( [0] => http://example.com/wp-content/uploads/2016/02/image12-80x80.png [1] => http://example.com/wp-content/uploads/2016/02/image11-80x80.png [2] => http://example.com/wp-content/uploads/2016/02/image10-80x80.png ) */
#2 Get a list of all images' SRCs from the first gallery of the post in the Block Editor
In the block editor (Gutenberg), the gallery has a different appearance-not a shortcode, so the function get_post_gallery_images() will return an empty array.
This problem is known and described in ticket #43826, but the solution will become part of the kernel only in the future, no exact date.
A temporary solution:
/**
* Returns all picture URLs of the first gallery from the specified post text.
* Returns the result if the gallery exists and is inserted as a block (Gutenberg).
*
* @param int|WP_Post $post
*
* @return array
*/
function get_post_block_gallery_images( $post = 0 ) {
$post = get_post( $post );
// Post not found - return empty array
if ( ! is_a( $post, 'WP_Post' ) ) {
return [];
}
// The "Gallery" block was not found - return an empty array
if ( ! has_block( 'gallery', $post->post_content ) ) {
return [];
}
// Search for all blocks in the content
$post_blocks = parse_blocks( $post->post_content );
// Go through all the blocks looking for the right one
foreach ( $post_blocks as $block ) {
// Search for the "Gallery" block with the passed IDs of the images
if ( $block['blockName'] === 'core/gallery' && ! empty( $block['attrs']['ids'] ) ) {
return array_map( function ( $image_id ) {
// Get a link to the original image
return wp_get_attachment_image_url( $image_id, 'full' );
}, $block['attrs']['ids'] );
}
}
// If the "Gallery" block is not found - return an empty array
return [];
} #3 Append the raw image URLs to the content
A simple example of how to append 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' );
/**
* Show image URLs below the content
*/
function wpdocs_show_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 the first gallery in the post
$gallery = get_post_gallery_images( $post );
$image_list = '<ul>';
// Loop through each image in each gallery
foreach( $gallery as $image_url ) {
$image_list .= '<li>' . '<img src="' . $image_url . '">' . '</li>';
}
$image_list .= '</ul>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
Notes
- See: get_post_gallery()
Changelog
| Since 3.6.0 | Introduced. |
get_post_gallery_images() get post gallery images code WP 6.9.1
function get_post_gallery_images( $post = 0 ) {
$gallery = get_post_gallery( $post, false );
return empty( $gallery['src'] ) ? array() : $gallery['src'];
}