get_post_gallery()WP 3.6.0

Gets the first gallery from the text of the specified post. Searches for the [gallery] shortcode in the text, processes it and returns an array of gallery image data.

Analog of the function get_post_galleries(), only gets the data of the first gallery, not all available.

Does not work with the block editor (Gutenberg). Works only for the [gallery] shortcode.

1 time — 0.014229 sec (extremely slow) | 50000 times — 612.03 sec (extremely slow) | PHP 7.0.4, WP 4.4.2
Hooks from the function

Returns

String|Array.

  • array - List of gallery images.
  • HTML string - If the $html parameter is true it will return a string - the HTML code of the gallery.
  • false - If the gallery is absent on the page.

Usage

get_post_gallery( $post, $html );
$post(int/WP_Post) (required)
ID or post object in the text of which the first gallery should be found.
$html(bool)
Return the gallery's ready HTML in the data or the gallery data as attachment IDs and links to the images of those attachments.
Default: true

Examples

0

#1 Get the first gallery of post 2179

$gal = get_post_gallery( 2179, false );

/* $gal will contain 
Array
(
	[ids] => 6790,6789,6788
	[src] => Array
		(
			[0] => http://wp-kama.ru/wp-content/uploads/2016/02/image12-80x80.png
			[1] => http://wp-kama.ru/wp-content/uploads/2016/02/image11-80x80.png
			[2] => http://wp-kama.ru/wp-content/uploads/2016/02/image10-80x80.png
		)

)
*/

Now let's set second parameter as true ($html = true):

$gal = get_post_gallery( 2179, true );

$gal will contain the gallery HTML string:

<div id='gallery-1' class='gallery galleryid-19 gallery-columns-3 gallery-size-thumbnail'><figure class='gallery-item'>
	<div class='gallery-icon landscape'>
		<a href='/article/sajt-na-konstruktore-wix/image12'>
			<img width="80" height="80" 
				src="/wp-content/uploads/2016/02/image12-80x80.png" 
				class="attachment-thumbnail size-thumbnail" alt="image12" /></a>
	</div></figure><figure class='gallery-item'>
	<div class='gallery-icon landscape'>
		<a href='/article/sajt-na-konstruktore-wix/image11'>
			<img width="80" height="80" 
				src="/wp-content/uploads/2016/02/image11-80x80.png" 
				class="attachment-thumbnail size-thumbnail" alt="image11" /></a>
	</div></figure><figure class='gallery-item'>
	<div class='gallery-icon landscape'>
		<a href='/article/sajt-na-konstruktore-wix/image10'>
			<img width="80" height="80" 
				src="/wp-content/uploads/2016/02/image10-80x80.png" 
				class="attachment-thumbnail size-thumbnail" alt="image10" /></a>
	</div></figure>
</div>
0

#2 Output each image in a gallery

<?php
/* The loop */
while ( have_posts() ) : the_post();

	if ( $gallery = get_post_gallery( get_the_ID(), false ) ) :

		// Loop through all the image and output them one by one.
		foreach ( $gallery['src'] AS $src ) {
			?>                
			<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
			<?php
		}

	endif;
endwhile;
?>

Changelog

Since 3.6.0 Introduced.

get_post_gallery() code WP 7.0

function get_post_gallery( $post = 0, $html = true ) {
	$galleries = get_post_galleries( $post, $html );
	$gallery   = reset( $galleries );

	/**
	 * Filters the first-found post gallery.
	 *
	 * @since 3.6.0
	 *
	 * @param array       $gallery   The first-found post gallery.
	 * @param int|WP_Post $post      Post ID or object.
	 * @param array       $galleries Associative array of all found post galleries.
	 */
	return apply_filters( 'get_post_gallery', $gallery, $post, $galleries );
}