WordPress.org

Codex

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

Function Reference/get post gallery

Description

Checks specified post's content for gallery and, if present, return the first gallery

Parameters

$post
(mixed) (optional) Post ID or Post object.
Default: 0
$html
(boolean) (optional) Whether to return HTML or data.
Default: true

Return

(string|array) 
Gallery data and srcs parsed from the expanded shortcode

Usage

 
<?php /* The loop */
while ( have_posts() ) : the_post();
    if ( 
get_post_gallery() ) :
        echo 
get_post_gallery();
    endif; 
endwhile; 
?>

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 ( get_post_gallery() ) :
            $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
            endforeach;
        endif;
    endwhile;
?>

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)

Change Log

Source File

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

Related

Function ref links

get_post_galleries(), get_post_galleries_images()

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