2

我正在尝试创建一个顶部菜单和一个子菜单,但为了防止在子菜单中重复顶部菜单,我将子菜单设置menu_slug为与顶部菜单中的相同。为什么在这种情况下根本不显示子菜单?

add_action("admin_menu", "setup_theme_admin_menus");    

function setup_theme_admin_menus() {    
    add_menu_page('Theme settings', 'Example theme', 'manage_options',   
        'tut_theme_settings', 'theme_settings_page');  

    add_submenu_page('tut_theme_settings',   
        'Front Page Elements', 'Front Page', 'manage_options',   
        'tut_theme_settings', 'theme_front_page_settings');   
}      

// Handler to top level menu
function theme_settings_page() {  
}  

function theme_front_page_settings() {
    echo "Some text of submenu page";  
}  
4

1 回答 1

5

这是默认行为,请参阅$menu_slug文档add_submenu_page

如果您不想复制父菜单项,则需要将其名称设置$menu_slug为与父 slug 完全相同。

问题是放置相同的 slug 会合并菜单和子菜单的回调。


您可能需要操纵全局$submenu变量来实现您的目标,请注意,我给子菜单提供了不同的 slug:

add_action( 'admin_menu', 'setup_theme_admin_menus' );    

function setup_theme_admin_menus() 
{    
    add_menu_page(
        'Theme settings', 
        'Example theme', 
        'manage_options',   
        'tut_theme_settings', 
        'theme_settings_page'
    );  

    add_submenu_page(
        'tut_theme_settings',       // parent slug
        'Front Page Elements 2',    // page title
        'Front Page 2',             // menu title
        'manage_options',           // capability
        'tut_theme_settings2',      // slug
        'theme_front_page_settings' // callback
    );  

    // REMOVE THE SUBMENU CREATED BY add_menu_page
    global $submenu;
    unset( $submenu['tut_theme_settings'][0] );
}  
于 2013-08-20T19:26:39.957 回答