使用Wordpress中的新图库编辑器,我可以设置图库并拖动缩略图以重新排列图像顺序。在后端,这似乎工作正常。但是,当我尝试使用“orderby”=>“menu\\u order”属性在前端显示库时,遇到了一个问题。显示器未使用menu\\u顺序。当我查看数据库时,我注意到对于所有库图像,我的附件的menu\\u order值仍然是“0”。因此,Wordpress 3.5中的重新排序和保存库似乎没有设置数据库中的menu\\u order值。这是虫子吗?还有其他人遇到过这个吗?如果是,您知道修复方法吗?
WordPress 3.5图库菜单顺序未设置?
在我看来,在阅读了源代码(PHP和JS)之后,这个库及其顺序根本没有保存到数据库中。Gallery仅在您创建JS时存在,甚至在您离开后期编辑页面时也不存在。
Gallery仅通过插入Gallery短代码(该短代码中声明的ID的准确顺序)来保存-不会将额外信息保存到数据库中,这都是关于该短代码的。
如果您想按菜单顺序循环帖子的附件,则必须为attachment post\\u type指定对插件或函数中页面属性的新支持。php,插入以下代码:
add_action(\'init\', \'my_custom_init\');
function my_custom_init() {
add_post_type_support( \'attachment\', \'page-attributes\' );
}
但是,坦率地说,通过输入每个附加编辑页面来指定菜单顺序是相当痛苦的。如果你需要按菜单顺序列出帖子的附件,WP 3.5的默认图库不适合你,我建议你使用一些经过验证的插件(如NextGen图库)或写下一个自定义插件,它可以让你从媒体上传器或库中设置菜单顺序。你是对的。
功能表\\u顺序不再用于多媒体资料中的媒体。不幸的是,库顺序的唯一“来源”似乎是嵌入在页面/帖子内容中的库短代码的“ids”参数。
不确定这是出于设计还是疏忽,但也可能是出于设计,因为您现在可以在图库中包含媒体,即使它没有“附加”到页面/帖子。在任何情况下,下面是我用来获取ID并根据快捷码中指定的顺序获取附件的方法。
关键是调用get\\u posts时的“orderby”参数必须是“post\\u in”,这告诉它按照“include”参数中指定的post id顺序进行排序。见下文。
// helper function to return first regex match
function get_match( $regex, $content ) {
preg_match($regex, $content, $matches);
return $matches[1];
}
// Extract the shortcode arguments from the $page or $post
$shortcode_args = shortcode_parse_atts(get_match(\'/\\[gallery\\s(.*)\\]/isU\', $post->post_content));
// get the ids specified in the shortcode call
$ids = $shortcode_args["ids"];
// get the attachments specified in the "ids" shortcode argument
$attachments = get_posts(
array(
\'include\' => $ids,
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'order\' => \'menu_order ID\',
\'orderby\' => \'post__in\', //required to order results based on order specified the "include" param
)
);
这并不理想,如果WP-core能够将这种排序存储在数据库中的某个地方,那就太好了,但在我们有一种更干净的方法之前,它是有效的。希望有帮助!
这只是我在最近的另一个项目中再次遇到的后续问题。我使用了用户在此处发布的解决方案(vickybiswas):
http://wordpress.org/support/topic/35-rc1-menu_order-not-saved-to-the-database-for-attached-images
这样做的好处是,它将向数据库中的项目添加菜单顺序。到目前为止,似乎对我有效,没有任何问题。以下是他提供的代码:
function pmc_gallery_menu_order_fix($id) {
$regex_pattern = get_shortcode_regex();
preg_match (\'/\'.$regex_pattern.\'/s\', stripslashes($_POST[\'content\']), $regex_matches);
if ($regex_matches[2] == \'gallery\') :
$attribureStr = str_replace (" ", "&", trim ($regex_matches[3]));
$attribureStr = str_replace (\'"\', \'\', $attribureStr);
$attributes = wp_parse_args ($attribureStr);
endif;
$ids = explode(\',\', $attributes[ids]);
$images = get_posts( array(
\'post_parent\' => $post->ID,
\'numberposts\' => \'-1\',
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'orderby\' => \'menu_order ID\',
\'order\' => \'ASC\'
) );
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
if (in_array($attachment->ID, $ids)) {
$update_post = array();
$update_post[\'ID\'] = $attachment->ID;
$update_post[\'menu_order\'] = array_search($attachment->ID, $ids);
wp_update_post( $update_post );
};
}
}
}
add_action(\'pre_post_update\', \'pmc_gallery_menu_order_fix\');
我有一个类似的问题,在您通过将图像拖动到新位置进行更改之前,似乎没有设置菜单顺序,然后将其异步保存到集中的所有图像。
菜单顺序似乎也仅限于特定帖子上的图片,尽管老实说,我没有和其他人玩过那么多。
希望有帮助。