Having Problem On Getting WP Post Gallery Images URL

As I’ve explained elsewhere, you can modify the image sizes of the get_post_gallery() or get_post_gallery_images() with a simple filter:

// Add a filter for full gallery size:
add_filter( 'shortcode_atts_gallery','wpse_full_size_gallery' );

$gallery = get_post_gallery( get_the_ID(), false );

 // Remove the filter:
remove_filter( 'shortcode_atts_gallery','wpse_full_size_gallery' );

where:

function wpse_full_size_gallery( $out )
{
    $out['size'] = 'full';  // Edit this to your needs! (thumbnail, medium, large, ...)
    return $out;
}

This saves you from running additional wp_get_attachment_image_src calls to retrieve the full size images from the attachments ids.

So your first code example would be:

<?php
     while ( have_posts() ) : the_post();
        if ( get_post_gallery() ) :

           // Add a filter for full gallery size:
           add_filter( 'shortcode_atts_gallery','wpse_full_size_gallery' );

           $gallery = get_post_gallery( get_the_ID(), false );

           // Remove the filter:
           remove_filter( 'shortcode_atts_gallery','wpse_full_size_gallery' );

           foreach( $gallery['src'] AS $src )
           {
                ?>
                <img src="https://wordpress.stackexchange.com/questions/175156/<?php echo $src; ?>" 
                     class="my-custom-class" 
                     alt="Gallery image" />
                <?php
           }
        endif;
    endwhile;
?>

to display the gallery images in full size.

Note that the get_children() function is fetching all attachments that were uploaded to a given post/posts. So in most cases, it will not give you the same result as the gallery shortcodes.

Leave a Comment

tech