要创建一个WP后台侧边栏的子菜单,我们需要用到 add_submenu_page 方法。
1、添加一个新菜单
// 添加一个新菜单
add_menu_page(__('My Menu Page'), __('My Menu'), 'edit_themes', 'my_new_menu', 'my_menu_render', '', 7);
function my_menu_render() {
global $title;
?>
<h2><?php echo $title;?></h2>
My New Menu Page!!
<?php
}
add_menu_page(__('My Menu Page'), __('My Menu'), 'edit_themes', 'my_new_menu', 'my_menu_render', '', 7);
function my_menu_render() {
global $title;
?>
<h2><?php echo $title;?></h2>
My New Menu Page!!
<?php
}
2、添加一个子菜单。
// 添加一个子菜单
add_submenu_page('my_new_menu', __('My SubMenu Page'), __('My SubMenu'), 'edit_themes', 'my_new_submenu', 'my_submenu_render');
add_submenu_page('my_new_menu', __('My SubMenu Page'), __('My SubMenu'), 'edit_themes', 'my_new_submenu', 'my_submenu_render');
function my_submenu_render() { global $title; ?> <h2><?php echo $title;?></h2> My New Menu Page!! <?php }
3、完整代码。将下列代码复制到 function.php 文件中即可。
function my_add_pages() {
add_menu_page(__('My Menu Page'), __('My Menu'), 'edit_themes', 'my_new_menu', 'my_menu_render', '', 7);
add_submenu_page('my_new_menu', __('My SubMenu Page'), __('My SubMenu'), 'edit_themes', 'my_new_submenu', 'my_submenu_render');
}
function my_menu_render() {
global $title;
?>
<h2><?php echo $title;?></h2>
My New Menu Page!!
<?php
}
function my_submenu_render() {
global $title;
?>
<h2><?php echo $title;?></h2>
My New Menu Page!!
<?php
}
add_menu_page(__('My Menu Page'), __('My Menu'), 'edit_themes', 'my_new_menu', 'my_menu_render', '', 7);
add_submenu_page('my_new_menu', __('My SubMenu Page'), __('My SubMenu'), 'edit_themes', 'my_new_submenu', 'my_submenu_render');
}
function my_menu_render() {
global $title;
?>
<h2><?php echo $title;?></h2>
My New Menu Page!!
<?php
}
function my_submenu_render() {
global $title;
?>
<h2><?php echo $title;?></h2>
My New Menu Page!!
<?php
}
add_action('admin_menu', 'my_add_pages');