代码不起作用的原因很可能是您之前已经对元数据库进行了排序,并且该顺序已保存到meta-box-order_page
元值。这将覆盖默认设置。
下面是meta-box-order_post
元值:
a:3:{
s:4:"side";
s:61:"submitdiv,formatdiv,categorydiv,tagsdiv-post_tag,postimagediv";
s:6:"normal";
s:96:"revisionsdiv,postexcerpt,trackbacksdiv,postcustom,
commentstatusdiv,commentsdiv,slugdiv,authordiv";
s:8:"advanced";
s:0:"";
}
这里,我刚刚重新格式化了序列化数组,以提高可读性。
粘性元盒:
要将给定元盒移动到顶部位置,对于给定的上下文和post类型,可以使用以下代码段:
/**
* Set some sticky metaboxes
*/
add_action( \'admin_init\', function()
{
if( function_exists( \'wpse_sticky_metabox\' ) )
{
// Sticky metabox #1:
wpse_sticky_metabox(
array(
\'cpt\' => \'page\',
\'context\' => \'normal\',
\'metabox\' => \'postexcerpt\'
)
);
// Sticky metabox #2:
wpse_sticky_metabox(
array(
\'cpt\' => \'post\',
\'context\' => \'side\',
\'metabox\' => \'authordiv\'
)
);
}
});
您可以根据自己的需要进行调整。
有关输入参数的一些信息:
cpt
是编辑屏幕的自定义贴子类型(即贴子、页面……)context
是要使元数据库粘滞的部分。(即正常、侧面、高级、…)metabox
是粘滞元框的id(即PostExtract、authordiv、…)
粘性元盒-插件:
以下演示插件支持此功能:
/**
* Plugin Name: Sticky Meta-Boxes
* Description: Set a given meta-box to the top, for a given cpt and context.
* Plugin URI: http://wordpress.stackexchange.com/a/174980/26350
* Author: Birgir Erlendsson (birgire)
* Version: 0.0.2
*/
function wpse_sticky_metabox( $args = array() )
{
if( class_exists( \'MetaBoxSticker\' ) )
{
$o = new MetaBoxSticker;
$o->setup( $args )->activate();
}
}
class MetaBoxSticker
{
private $args;
public function setup( $args = array() )
{
$default = array(
\'cpt\' => \'post\',
\'context\' => \'normal\',
\'metabox\' => \'postexcerpt\'
);
$this->args = wp_parse_args( $args, $default );
return $this;
}
public function activate()
{
add_filter(
sprintf(
\'get_user_option_meta-box-order_%s\',
$this->args[\'cpt\']
),
array( $this, \'filter\' ),
PHP_INT_MAX
);
add_action(
\'add_meta_boxes\',
array( $this, \'relocate\' ),
PHP_INT_MAX
);
}
public function relocate()
{
//-----------------------
// Get the user input:
//-----------------------
$_cpt = sanitize_key( $this->args[\'cpt\'] );
$_metabox = sanitize_key( $this->args[\'metabox\'] );
$_context = sanitize_key( $this->args[\'context\'] );
//-----------------------
// Relocate \'high\' metaboxes to \'default\' in the current context
//-----------------------
global $wp_meta_boxes;
if( isset( $wp_meta_boxes[$_cpt][$_context][\'high\'] ) )
{
foreach( $wp_meta_boxes[$_cpt][$_context][\'high\'] as $id => $item )
{
if( isset( $item[\'callback\'] ) )
{
remove_meta_box(
$id,
$_cpt,
$_context
);
add_meta_box(
$id,
$item[\'title\'],
$item[\'callback\'],
$_cpt,
$_context,
\'default\',
$item[\'args\']
);
}
}
}
}
public function filter( $order )
{
//-----------------------
// Get the user input:
//-----------------------
$_cpt = sanitize_key( $this->args[\'cpt\'] );
$_metabox = sanitize_key( $this->args[\'metabox\'] );
$_context = sanitize_key( $this->args[\'context\'] );
//-----------------------
// Handle the case if the current user hasn\'t made any meta-box ordering before:
//-----------------------
if( empty( $order ) )
{
global $wp_meta_boxes;
if( ! isset( $wp_meta_boxes[$_cpt][$_context] ) )
return $order;
$order = array();
foreach( $wp_meta_boxes[$_cpt] as $context_key => $context_item )
{
$tmp = array();
foreach( $context_item as $priority_key => $priority_item )
{
foreach( $priority_item as $metabox_key => $metabox_item )
{
$tmp[] = $metabox_key;
}
}
$order[$context_key] = join( \',\', $tmp );
}
}
//-----------------------
// Let\'s make sure the context exists:
//-----------------------
if( ! isset( $order[$_context] ) )
return $order;
//-----------------------
// Remove the given meta-box from the whole order array:
//-----------------------
foreach( $order as $context_key => $string )
{
$tmp = explode( \',\', $string );
$key = array_search( $_metabox, $tmp );
if( ! empty( $key ) )
{
unset( $tmp[$key] );
$order[$context_key] = join( \',\', $tmp );
}
}
//-----------------------
// Make the given meta-box sticky for a given context
//-----------------------
$tmp = explode( \',\', $order[$_context] );
array_unshift( $tmp, $_metabox );
$order[$_context] = join( \',\', $tmp );
return $order;
}
} // end class
这个插件也应该可以工作,即使你以前没有订购过。
它还尊重屏幕选项,即元框是否可见。
我希望你能进一步满足你的需要。