按菜单顺序排序获取子项(_O)

时间:2013-02-07 作者:focus97

我正在使用jQuery Carousel库,不幸的是,它不能与WP的新v3一起使用。5画廊订购。当客户端拖动以重新排列图像时,这些更改不会反映在前端库中。遗憾的是,插件不再维护。所以我潜入并能看到它在使用get_children.

我看到了:

$attachments = get_children(
    array(
        \'post_parent\' => $id,
        \'post_status\' => \'inherit\',
        \'post_type\' => \'attachment\',
        \'post_mime_type\' => \'image\',
        \'orderby\' => \'menu_order\'
    )
);
还有这个:

$js = array();
foreach ( $attachments as $id => $attachment ) {
    $image = wp_get_attachment_image_src($id, "full");
    // $image = the_attachment_link( $attachment->ID , false );
    $js[] = "{url: \'" . $image[0] . "\', title: \'".addslashes($attachment->post_title)."\', caption: \'".addslashes(remove_brs($attachment->post_excerpt))."\', description: \'".addslashes(remove_brs($attachment->post_content))."\'}";
}
$output .= join(",\\n", $js);
$output .= "];\\n";`
我在这里和其他地方看到,get\\u帖子将遵循menu\\u顺序orderby 参数,因此为了进行尝试,我进行了以下尝试:

$post_img_args = array(
    \'post_type\' => \'attachment\',
    \'numberposts\' => -1,
    \'post_status\' =>\'any\',
    \'post_parent\' => $post->ID,
    \'orderby\' => \'menu_order\',
    \'post_mime_type\' => \'image\'
);
$attachments = get_posts($post_img_args);
foreach 上述声明,我替换了$image 变量为

$image = the_attachment_link( $attachment->ID , false );
这摧毁了前端的画廊。(请注意,禁用插件并在前面查看可以很好地显示图像顺序,因此我相信这取决于上面的代码及其输出)。

任何见解或指导都将不胜感激。谢谢

1 个回复
SO网友:Bainternet

在WordPress 3.5之后,默认情况下,gallery短代码现在包括图像ID。像这样[gallery ids="729,732,731,720"] 它也保留了顺序,所以打开名为carousel-gallery-jquery.php 并替换这一行:(第140行附近)

$attachments = get_children( array(\'post_parent\' => $id, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
使用此选项:

if ( isset($include) && !empty($include) ) {
        $_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];
        }
} elseif ( isset($exclude) && !empty($exclude) ) {
        $attachments = get_children( array(\'post_parent\' => $id, \'exclude\' => $exclude, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
} else {
        $attachments = get_children( array(\'post_parent\' => $id, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
}
然后在第121行该函数的顶部添加以下内容:

if ( ! empty( $attr[\'ids\'] ) ) {
    // \'ids\' is explicitly ordered, unless you specify otherwise.
    if ( empty( $attr[\'orderby\'] ) )
            $attr[\'orderby\'] = \'post__in\';
    $attr[\'include\'] = $attr[\'ids\'];
}
你应该没事:)

结束

相关推荐