get_post_gallery_images( int|WP_Post $post )

Checks a post’s content for galleries and return the image srcs for the first found gallery


Description Description

See also See also


Top ↑

Parameters Parameters

$post

(int|WP_Post) (Optional) Post ID or WP_Post object. Default is global $post.


Top ↑

Return Return

(array) A list of a gallery's image srcs in order.


Top ↑

Source Source

File: wp-includes/media.php

function get_post_gallery_images( $post = 0 ) {
	$gallery = get_post_gallery( $post, false );
	return empty( $gallery['src'] ) ? array() : $gallery['src'];
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.6.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Example
    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;
     }
    
    

You must log in before being able to contribute a note or feedback.