get_post_gallery( int|WP_Post $post, bool $html = true )
Check a specified post’s content for gallery and, if present, return the first
Description Description
Parameters Parameters
Return Return
(string|array) Gallery data and srcs parsed from the expanded shortcode.
Source Source
File: wp-includes/media.php
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 );
}
Expand full source code Collapse full source code View on Trac
Changelog Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
If there is no gallery present in the page, the function will return
false(boolean).Example
Output each image in a gallery with our own custom image class when using data output and not HTML:
<?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; ?>Expand full source codeCollapse full source code
Example data output
array (size=3) 'link' => string 'file' (length=4) 'ids' => string '423,424,425,426' (length=15) 'src' => array (size=4) 0 => string 'http://www.yoursite.com/wp-content/uploads/2013/10/1296136694836-150x150.jpg' (length=85) 1 => string 'http://www.yoursite.com/wp-content/uploads/2013/10/1315981706292-150x150.jpg' (length=85) 2 => string 'http://www.yoursite.com/wp-content/uploads/2013/10/fate-stay-night-446-wall1200-150x150.jpg' (length=100) 3 => string 'http://www.yoursite.com/wp-content/uploads/2013/10/GpTq3-150x150.jpg' (length=77)