这里有一个例子;
首先,要根据子菜单项的数组键确定子菜单项的顺序,可以执行以下操作var_dump
在$子菜单上,全局变量将输出以下内容:;
(我以Posts菜单和sub菜单为例)
//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。
例如,如果我想添加子菜单项,请直接在All Posts 子菜单项我需要通过将数组键设置为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:将其添加到函数中。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....
);
如果子菜单项具有相同的键(位置),此修订将防止子菜单项相互过度写入,因为它将循环使用,直到找到不存在的可用键(位置)。