Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm noticing that adding a gallery to a page now just lists the ID's of images, slightly different to before, and reordering the images just amends the order of the ID's listed in the gallery short-code.

So this...

[gallery order="DESC" link="file" columns="4"]

It's now this...

[gallery ids="3439,3440,3444,3443,3445,3453,3457,3458,3456,3461,3452,3468,3467,3470,3464,3459,3463,3462,3460,3466,3454,3450,3449,3448,3451,3447,3446,3442,3441,3438,3471,3469"]

Which is fine, and old galleries created in previous versions still appear to work. My problem is I have a summary page grabbing the first image of every child page. The code is something like this....

                                                            $args = array(
                                                            'post_type' => 'attachment',
                                                            'post_parent' => $page->ID,
                                                            'orderby' => 'menu_order ID',
                                                            'order'=> 'asc',
                                                            'numberposts' => 1,
                                                            );
                                                        $attachments = get_posts($args);

This now doesn't work (I'm guessing because gallery order is now based on the ID's) Any way to fix it? Thanks

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I had the same issue and was previously using get_children() to retrieve the top 4 images from a gallery for a given page.

Here's the solution I came up with:

// helper function to return first regex match
function get_match( $regex, $content ) {
    preg_match($regex, $content, $matches);
    return $matches[1];
} 

// Extract the shortcode arguments from the $page or $post
$shortcode_args = shortcode_parse_atts(get_match('/\[gallery\s(.*)\]/isU', $post->post_content));

// get the attachments specified in the "ids" shortcode argument
$attachments = get_posts(
    array(
        'include' => $shortcode_args["ids"], 
        'post_status' => 'inherit', 
        'post_type' => 'attachment', 
        'post_mime_type' => 'image', 
        'order' => 'menu_order ID', 
        'orderby' => 'post__in', //this forces the order to be based on the order of the "include" param
        'numberposts' => 1,
    )
);

Now, 'numberposts' => 1 above doesn't seem to work when you specify more than one id in the include parameter. So, you have a couple of choices:

  1. Explode the "ids" argument and grab only the first ID for use in your get_posts call
  2. Grab only the first attachment returned from get_posts (but you'll still be retrieving all attachments in the gallery). Not ideal, but will work.

Hope that helps!

share|improve this answer
    
thanks! just what I needed :) –  numediaweb Mar 8 '13 at 20:10
    
Glad that worked! If that answers your question, would you mind marking it as "answer" in addition to the plus one? Thanks! –  anderly Mar 9 '13 at 22:21
    
Thanks very much anderly! –  chael Mar 11 '13 at 3:14
    
One quick question, I've managed to make this work (grabbing one image from each post) however it's the last image in the list of ID's instead of the first? –  chael Mar 11 '13 at 10:36
    
So, using the sample above, are you saying that $attachments[0] above is the last image in the list of IDs? –  anderly Mar 12 '13 at 3:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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