0

I'm grabbing URLs of my Gallery images:

$gallery = get_post_gallery_images( $post );

foreach($gallery as $thumbnail) {
   echo $thumbnail_slideshow;
}   

The problem is all I'm getting are thumbnails:

image1-150x150.jpg
image2-150x150.jpg
image3-150x150.jpg

And I want to get:

image1-600x100.jpg
image2-600x100.jpg
image3-600x100.jpg

Of course I have my custom image_size defined in functions.php:

add_image_size( 'mysize', 660, 100, true ); 

And I'm wondering if it is possible to do the easy way using get_post_gallery( $post ) or should create some kind of a loop?

Here's a great answer by Otto, unfortunately it doesn't work for me (I'm trying to make it work in single-customposttype.php, it displays nothing, replacing $post->ID with $page->ID or $parent->ID displays EVERYTHING from Media Library ).

UPDATE

Looks like I don't understand something right.

I took a snippet I've seen around in many places, also in Otto's post above:

$gallery_images = new WP_Query(array(
        'post_parent' => $post->ID,
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'post_status' => 'inherit',
));

while ( $gallery_images->have_posts() ) : $gallery_images->the_post();
        the_title(); 
        the_content(); 
        the_permalink();
endwhile;

I pasted it into content.php of Twenty Thirteen and guess what? It returns NOTHING, and of course this post HAS gallery and get_post_gallery_images($post) work on it flawlessly. What am I doing wrong?

Of course when I UPLOAD image to the gallery code above works, because the image is uploaded / attached directly to this post. All of my other images are of course "(Unattached)" and this might be a problem...

UPDATE II

Got it, finally. Looks like Gallery images used to be attached to posts in WP pre-3.5, now it doesn't work this way. Here's it well explained. So the question is - is this solution good and is it safe to use it for the future? Of maybe it's possible to play with get_post_gallery_images sizes? To be honest this function would be so much more powerful if it would load all image sizes in multidimensional array...

2 Answers 2

2

Use the shortcode_atts_gallery filter to override the size of the images used before making your call to get_post_gallery_images.

add_filter('shortcode_atts_gallery','force_large_images',10,3);
function force_large_images($out, $pairs, $atts) {
  $out['size'] = 'large';
  return $out;
}

Make sure to remove the filter when you have your data, or all your galleries will always use large images.

remove_filter('shortcode_atts_gallery','force_large_images',10,3);
2

Two Method in here, if you would like to customize default gallery shortcode, you have to re-edit shortcode!. I think just for now, you are only extract gallery image! So, here is my solution

<?php 

$args = array( 'post_mime_type' => 'image', 'post_type' => 'attachment', 'post_parent' => $post->ID, 'order' => 'ASC');
$attachments = get_children($args);

foreach( $attachments as $attachment) { 
    if ( !empty($attachment->guid ) ) {
        $img_url = wp_get_attachment_image_src($attachment->ID, 'full'); //change *full* size with your custom size

?>

1
  • I was thinking about that, but it's not that simple as I'm going to need different gallery sizes for different pages / posts and custom post types... Dec 9, 2013 at 17:07

Your Answer

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

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