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 回答