I've added image gallery to my page via Add Media > Create Gallery. I don’t want to output default html of the gallery, neither are sufficient options to change gallery sortcode's attributes: itemtag, icontag, captiontag as I want an entirely custom html.

I’ve tried to filter post_gallery, grab urls of images that are included as a gallery into that page and output them:

add_filter( 'post_gallery', 'my_name_page_gallery', 10, 2 );
function my_name_page_gallery($output, $attr) {
    $output = get_post_gallery_images();
    return $output;

But I get the following error:

Maximum function nesting level of ‘512’ reached, aborting! in /wp-includes/shortcodes.php on line 234

What am I doing wrong? How do I get gallery images’ urls and output custom html?

1 Answer 1


Is this what your looking for, here? get_post_gallery_images().

This is the example for that page.

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.