1

ご覧のとおり、ギャラリーを希望の順序で並べ替えるのに苦労しています。ギャラリーを編集するドラッグ アンド ドロップ インターフェイスと同じように並べ替えようとしています。これは、ショートコードの id 属性と同じ順序です。$orderby に割り当てる値がわかりません。「ID」、「menu_order」、および「post__in」を試しましたが、変更はありません。何かアドバイスはありますか。

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;
}

ありがとう。さらなるドキュメントへのヒントもありがたかったです。

4

1 に答える 1

0

wp-includes/media.php ファイルで必要なものを見つけました

「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;
}

私のために働きます。

于 2013-02-09T23:46:12.807 に答える