要查看当前阵列键,请尝试以下操作:
add_action( \'admin_menu\' , \'admin_menu_new_items\', 1 );
function admin_menu_new_items() {
global $submenu;
wp_die( \'<pre>\' . var_export( $submenu[\'edit.php\'], true ) . \'</pre>\' );
}
我明白了:
array (
5 =>
array (
0 => \'All Posts\',
1 => \'edit_posts\',
2 => \'edit.php\',
),
10 =>
array (
0 => \'Add New\',
1 => \'edit_posts\',
2 => \'post-new.php\',
),
15 =>
array (
0 => \'Categories\',
1 => \'manage_categories\',
2 => \'edit-tags.php?taxonomy=category\',
),
16 =>
array (
0 => \'Tags\',
1 => \'manage_categories\',
2 => \'edit-tags.php?taxonomy=post_tag\',
),
)
索引“10”是添加新项。要添加新的子菜单项,请使用索引11、12和13:
$submenu[\'edit.php\'][11] = array( \'Add New Profile\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=pitches\' );
$submenu[\'edit.php\'][12] = array( \'Add New News\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=news\' );
$submenu[\'edit.php\'][13] = array( \'Add New Pitch\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=pitch\' );
当WordPress将子菜单项添加到它处理的仪表板时
$submenu
作为关联数组。添加到
edit.php
无论使用的键是什么,数组都将添加到数组的末尾。使用
ksort() 订购钥匙。
ksort( $submenu[\'edit.php\'], SORT_NUMERIC );
总而言之:
add_action( \'admin_menu\' , \'admin_menu_new_items\', 1 );
function admin_menu_new_items() {
global $submenu;
$submenu[\'edit.php\'][11] = array( \'Add New Profile\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=pitches\' );
$submenu[\'edit.php\'][12] = array( \'Add New News\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=news\' );
$submenu[\'edit.php\'][13] = array( \'Add New Pitch\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=pitch\' );
// WordPress treats $submenu as an associative array and does not sort it first.
// We have to sort keys into the order we want them to show up.
ksort( $submenu[\'edit.php\'], SORT_NUMERIC );
}