I need to get the URL (actual Size) of all images of gallery associated with a post in single.php. The following code from Codex returns all images in very small size 150×150 which is not suitable for what I want

<?php
     while ( have_posts() ) : the_post();
        if ( get_post_gallery() ) :
            $gallery = get_post_gallery( get_the_ID(), false );
           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;
?>

Then I find following method which retuns the images in actual size BUT return all images in the Galleries (Not only images within the galley of current Post)

<?php
     global $post;
     $thumbnail_ID = get_post_thumbnail_id();
     $images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
     print_r( $images );
     if ($images) :

         foreach ($images as $attachment_id => $image) :

         if ( $image->ID != $thumbnail_ID ) :

             $img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
             if ($img_alt == '') : $img_alt = $image->post_title; endif;

             $big_array = image_downsize( $image->ID, 'large' );
             $img_url = $big_array[0];

             echo '<li>';
             echo '<img src="';
             echo $img_url;
             echo '" alt="';
             echo $img_alt;
             echo '" />';
             echo '</li><!--end slide-->';

     endif; endforeach; endif;

so I tried to retrieve this by adding $post_id = get_the_ID(); to cede as:

<?php
     global $post;
     $thumbnail_ID = get_post_thumbnail_id();
      $post_id = get_the_ID();
     $images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
     print_r( $images );
     if ($images) :

         foreach ($images as $attachment_id => $image) :

         if ( $image->ID != $thumbnail_ID ) :

             $img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
             if ($img_alt == '') : $img_alt = $image->post_title; endif;

             $big_array = image_downsize( $image->ID, 'large' );
             $img_url = $big_array[0];

             echo '<li>';
             echo '<img src="';
             echo $img_url;
             echo '" alt="';
             echo $img_alt;
             echo '" />';
             echo '</li><!--end slide-->';

     endif; endforeach; endif;

but now I am getting only white page without any on the page! Can you please let me know why this is happening and how I can fix this? Thansk

1 Answer
1

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 Reply

Your email address will not be published. Required fields are marked *