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 have the following gallery order in a shortcode:

[gallery ids="1996,2431,372,447,645,641,647,642,649,643,650,648,653,697,652,390"]

And I have this function, which creates a nicer looking gallery:

add_filter('post_gallery', 'pab_gallery'); 

function pab_gallery($attr) {

    global $post;

    extract(shortcode_atts(array(
        //'order'      => 'ASC',
        'orderby'    => 'menu_order DESC, ID ASC',
        'id'         => $post->ID,
        'columns'    => 3,
        'size'       => 'thumbnail',
        'include'    => '',
        'exclude'    => ''
    ), $attr));

    $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => $orderby) );

    #print_r($attachments);


    $output = '<div class="gallery">';
    $output .= '<ul class="clearfix">';
    foreach ( $attachments as $id => $attachment ) {
        $link = wp_get_attachment_link($id, $size, false, true);
        $output .= sprintf('<li>%s</li>', $link);
    }

    return $output.'</ul></div>';
}

I have two problems with this: First of all, the order specified by ID is completely ignored, even though I pass "orderby". Additionally, image#1996 is also not displayed by the function, even though it's attached to the post (which I thought isn't necessary anyway when providing IDs to the short codes). This is the order I receive from the plugin :

Array ( [0] => 2431 [1] => 656 [2] => 652 [3] => 651 [4] => 648 [5] => 650 [6] => 643 [7] => 649 [8] => 642 [9] => 647 [10] => 641 [11] => 645 [12] => 629 [13] => 676 )

As you can see it's neither the order I specified using the shortcodes, nor is it in any way ascending or descending.

I want the function to display the IDs in the exact order that they're specified in the shortcodes. Is this possible?

share|improve this question

1 Answer 1

Since 3.5, the gallery shortcode works a bit differently, the order is embedded within the shortcode itself, in the form of the ids= attribute. In your call to get_posts, change orderby to post__in, and pass the ids as include argument.

For more specifics, look inside wp-includes/media.php at the gallery_shortcode function to see how core does it.

share|improve this answer
    
How do I retrieve the IDs from the shortcode? –  doque May 16 '13 at 9:38

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.