1

The WordPress gallery shortcode now looks like this:

[gallery ids="3439,3440,3444,3443,3445,3453,3457,3458,3456,3461,3452,3468"]

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?

1 Answer 1

4

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!

6
  • 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, 2013 at 22:21
  • 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, 2013 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, 2013 at 3:45
  • Another option would be to explode the list of IDs like this: $ids = explode(',', $shortcode_args["ids"]); Then, you can use $ids[0] in the call get_posts instead of passing in all the IDs with $shortcode_args["ids"].
    – anderly
    Mar 12, 2013 at 3:46
  • Thanks so much - it works perfectly. Turns out my $shortcode_args wasn't working because I needed $page->post_content instead of $post->post_content, resulting in a random image from the pages gallery.
    – chael
    Mar 14, 2013 at 12:58

Your Answer

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

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