如果菜单slug与顶层菜单中的相同,则add_submenu_page不显示子菜单

时间:2013-08-20 05:20:13

标签: wordpress

我正在尝试创建顶级菜单和子菜单,但为了防止重复子菜单中的顶级菜单,我将子菜单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";  
}  

1 个答案:

答案 0 :(得分:4)

这是默认行为,请参阅add_submenu_page的{​​{3}}:

  

如果您不想复制父菜单项,则需要将$menu_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] );
}