这是一个例子。
首先,根据其数组键确定子菜单项的顺序,您可以var_dump
在$ submenu全局变量上执行a ,该变量将输出以下内容;
(我以“帖子”菜单和子菜单为例)
//shortened for brevity....
["edit.php"]=>
array(6) {
[5]=>
array(3) {
[0]=> string(9) "All Posts"
[1]=> string(10) "edit_posts"
[2]=> string(8) "edit.php"
}
[10]=>
array(3) {
[0]=> string(7) "Add New"
[1]=> string(10) "edit_posts"
[2]=> string(12) "post-new.php"
}
[15]=>
array(3) {
[0]=> string(10) "Categories"
[1]=> string(17) "manage_categories"
[2]=> string(31) "edit-tags.php?taxonomy=category"
}
[17]=>
array(3) {
[0]=> string(14) "Sub Menu Title"
[1]=> string(10) "edit_posts"
[2]=> string(17) "sub_menu_page.php"
}
}
我们可以看到我的子菜单项被添加到数组中,默认项之后的键为17。
例如,如果要添加子菜单项,则需要在“ 所有帖子”子菜单项之后直接添加我的数组,方法是将阵列键设置为6、7、8或9(分别在5和10之前的任何值)。
这就是你的做法...
function change_submenu_order() {
global $menu;
global $submenu;
//set our new key
$new_key['edit.php'][6] = $submenu['edit.php'][17];
//unset the old key
unset($submenu['edit.php'][17]);
//get our new key back into the array
$submenu['edit.php'][6] = $new_key['edit.php'][6];
//sort the array - important! If you don't the key will be appended
//to the end of $submenu['edit.php'] array. We don't want that, we
//our keys to be in descending order
ksort($submenu['edit.php']);
}
结果,
["edit.php"]=>
array(6) {
[5]=>
array(3) {
[0]=> string(9) "All Posts"
[1]=> string(10) "edit_posts"
[2]=> string(8) "edit.php"
}
[6]=>
array(3) {
[0]=> string(14) "Sub Menu Title"
[1]=> string(10) "edit_posts"
[2]=> string(17) "sub_menu_page.php"
}
[10]=>
array(3) {
[0]=> string(7) "Add New"
[1]=> string(10) "edit_posts"
[2]=> string(12) "post-new.php"
}
[15]=>
array(3) {
[0]=> string(10) "Categories"
[1]=> string(17) "manage_categories"
[2]=> string(31) "edit-tags.php?taxonomy=category"
}
}
...尝试一下,让我们知道您的前进方向!
更新1:
将此添加到您的functions.php文件中;
function change_post_menu_label() {
global $menu;
global $submenu;
$my_menu = 'example_page'; //set submenu page via its ID
$location = 1; //set the position (1 = first item etc)
$target_menu = 'edit.php'; //the menu we are adding our item to
/* ----- do not edit below this line ----- */
//check if our desired location is already used by another submenu item
//if TRUE add 1 to our value so menu items don't clash and override each other
$existing_key = array_keys( $submenu[$target_menu] );
if ($existing_key = $location)
$location = $location + 1;
$key = false;
foreach ( $submenu[$target_menu] as $index => $values ){
$key = array_search( $my_menu, $values );
if ( false !== $key ){
$key = $index;
break;
}
}
$new['edit.php'][$location] = $submenu[$target_menu][$key];
unset($submenu[$target_menu][$key]);
$submenu[$target_menu][$location] = $new[$target_menu][$location];
ksort($submenu[$target_menu]);
}
我的更新包括一个稍微简单一些的方法来处理菜单位置的设置,您只需要指定子菜单页面的名称以及菜单中所需的位置即可。但是,如果选择的子菜单页$location
与现有键的子菜单页相同,它将用您的键覆盖该键,因此菜单项将消失,而菜单项将位于其位置。在这种情况下,增加或减少数字以正确排序菜单。类似地,如果有人安装了一个效果相同的菜单区域且其$location
子菜单项与之相同的插件,则将发生相同的问题。为了避免这种情况,Kaiser的示例对此进行了一些基本检查。
更新2:
我添加了一个额外的代码块,用于检查数组中所有现有键$location
是否与我们期望的键相符;如果找到匹配项,则将增加我们的$location
值,1
以避免菜单项相互覆盖。这是负责的代码,
//excerpted snippet only for example purposes (found in original code above)
$existing_key = array_keys( $submenu[$target_menu] );
if ($existing_key = $location)
$location = $location + 1;
更新3 :(对脚本进行了修改,以允许对多个子菜单项进行排序)
add_action('admin_init', 'move_theme_options_label', 999);
function move_theme_options_label() {
global $menu;
global $submenu;
$target_menu = array(
'themes.php' => array(
array('id' => 'optionsframework', 'pos' => 2),
array('id' => 'bp-tpack-options', 'pos' => 4),
array('id' => 'multiple_sidebars', 'pos' => 3),
)
);
$key = false;
foreach ( $target_menu as $menus => $atts ){
foreach ($atts as $att){
foreach ($submenu[$menus] as $index => $value){
$current = $index;
if(array_search( $att['id'], $value)){
$key = $current;
}
while (array_key_exists($att['pos'], $submenu[$menus]))
$att['pos'] = $att['pos'] + 1;
if ( false !== $key ){
if (array_key_exists($key, $submenu[$menus])){
$new[$menus][$key] = $submenu[$menus][$key];
unset($submenu[$menus][$key]);
$submenu[$menus][$att['pos']] = $new[$menus][$key];
}
}
}
}
}
ksort($submenu[$menus]);
return $submenu;
}
在上面的示例中,可以通过在$target_menu
包含多维值数组的变量内相应地设置参数,来定位多个子菜单和每个子菜单多个项目。
$target_menu = array(
//menu to target (e.g. appearance menu)
'themes.php' => array(
//id of menu item you want to target followed by the position you want in sub menu
array('id' => 'optionsframework', 'pos' => 2),
//id of menu item you want to target followed by the position you want in sub menu
array('id' => 'bp-tpack-options', 'pos' => 3),
//id of menu item you want to target followed by the position you want in sub menu
array('id' => 'multiple_sidebars', 'pos' => 4),
)
//etc....
);
此修订将防止子菜单项具有相同的键(位置)时相互覆盖,因为它将循环滚动直到找到不存在的可用键(位置)。