1

as you can read, I'm having trouble sorting the gallery in the order I want it to. I'm trying to have it sorted just like in the Drag&Drop Interface, where you edit your gallery. That's the same order as in the id attribute in the shortcode. I just can't figure out what value to assign to $orderby. i tried 'ID', 'menu_order' and 'post__in', but no changes. Do you have any advice.

add_filter('post_gallery', 'fgf_gallery', 10, 2);

function fgf_gallery($output, $attr) {
    global $post;

    static $instance = 0;
    $instance++;

    $id = $post->ID;
    $order = 'ASC';
    $orderby = 'ID'; 

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

    if ( empty($attachments) ) return;  

    $output = "<ul id='gallery-{$instance}' class='gallery'>";
    foreach ( $attachments as $id => $attachment ) {
        $output .= "<li class='gallery-item'>";
        $output .= "<img src='".$thumb_src."'>";

        $output .= "<h1>".$attachment->post_title."</h1>";

        if ( trim($attachment->post_excerpt) ) {
            $output .= "
            <p class='wp-caption-text gallery-caption'>
            " . wptexturize($attachment->post_excerpt) . "
            <p>";
        }
        $output .= "</li>";
    }
    $output .= "</ul>\n";
    return $output;
}

Thanks. I appreciated any hint to further documentation as well.

yardarrat
  • 280
  • 3
  • 14

1 Answers1

0

I found what I needed in the wp-includes/media.php file

you get the order from your shortcode with "post__in".

function fgf_gallery_2($output, $attr) {

    static $instance = 0;
    $instance++;

    $order = 'ASC';

    $orderby = 'post__in';
    $include = $attr['ids'];

    $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
    $attachments = array();
    foreach ( $_attachments as $key => $val ) {
        $attachments[$val->ID] = $_attachments[$key];
    }

    if ( empty($attachments) ) return;  

    $output = "<ul id='gallery-{$instance}' class='gallery'>";
    foreach ( $attachments as $id => $attachment ) {
        /* do what you want here */
    }
    $output .= "</ul>\n";
    return $output;
}

works for me.

yardarrat
  • 280
  • 3
  • 14