get_post_galleries_images()
Gets all image URLs from galleries in the specified post text, if galleries are present in the text.
For example, if there are 2 galleries in the text - 2 shortcodes [gallery], the function will return all image URLs from these two galleries.
Gets thumbnail image URLs, not full-size images.
When you need to process only the first gallery, use get_post_gallery_images().
Does not work with the block editor (Gutenberg). Works only for the shortcode [gallery].
No Hooks.
Returns
Array. A list of image URLs from each gallery. Each list will be a nested array. See example.
Usage
get_post_galleries_images( $post );
- $post(int/WP_Post)
- ID/object of the post whose gallery image links need to be retrieved.
Default: current post
Examples
#1 Get a list of all image's src from all galleries of post 2179
Let's say there are 2 galleries in the post 2179 - 2 shortcodes [gallery] and we need to get all the links of these galleries' pictures. In the shortcode we specify the IDs of these images attachments:
$gal = get_post_galleries_images( 2179 ); /* $gal will contain Array ( [0] => Array ( [0] => http://wp-kama.com/wp-content/uploads/2016/02/image12-80x80.png [1] => http://wp-kama.com/wp-content/uploads/2016/02/image11-80x80.png [2] => http://wp-kama.com/wp-content/uploads/2016/02/image10-80x80.png ) [1] => Array ( [0] => http://wp-kama.com/wp-content/uploads/2016/02/image008-80x80.jpg [1] => http://wp-kama.com/wp-content/uploads/2016/02/image007-80x80.jpg [2] => http://wp-kama.com/wp-content/uploads/2016/02/image006-80x80.jpg ) ) */
#2 Append the image URLs to the post content if post has gallery in 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' );
/**
* Add list of image URLs to the content if displaying
* a post with one or more image galleries.
*
* @param string $content Post content.
*
* @return string (Maybe modified) post 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 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;
}
Notes
- See: get_post_galleries()
Changelog
| Since 3.6.0 | Introduced. |
get_post_galleries_images() get post galleries images code WP 6.9.1
function get_post_galleries_images( $post = 0 ) {
$galleries = get_post_galleries( $post, false );
return wp_list_pluck( $galleries, 'src' );
}