WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get post gallery images

Description

Retrieves an array of image URLs that belong to the first gallery added to the specified post.

Parameters

$post
(Post ID or object.) (required) The post to look in.
Default: None

Return

(array) 
An array of image URLs.

Usage

 <?php  $gallery get_post_gallery_images$post ); ?> 

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.

 function pw_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;

 }
 add_filter( 'the_content', 'pw_show_gallery_image_urls' );

Change Log

Source File

get_post_gallery_images() is located in wp-includes/media.php.

Related

get_post_gallery(), get_post_galleries(), get_post_galleries_images()

This article is marked as in need of editing. You can help Codex by editing it.