3

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 150x150 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="<?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

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="<?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.

  • great, thanks birgire, I just have two questions for you. First: what is the point of removing the filter at remove_filter( 'shortcode_atts_gallery','wpse_full_size_gallery' ); ? is there any concern on doing this? And can you please let me know where should I put the function wpse_full_size_gallery( $out ){} ? in the single.php or function.php ? Thanks – Mona Coder Jan 17 '15 at 11:19
  • Hi @MonaCoder, yes you can put the function into the functions.php file in your current theme directory. It's a good common housekeeping rule to remove the filter callback again with remove_filter(), just in case you have another gallery shortcode later on the page. – birgire Jan 17 '15 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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