Main menu link also showing on submenu in WP admin

2019-02-25 11:49发布

问题:

I have created a custom top menu in admin panel and also add two submenu for that menu. my code is as follows:

add_action('admin_menu', 'my_create_menu');

function my_create_menu() {
    add_menu_page('My custom menu', 'My custom menu Settings', 'manage_options', __FILE__, 'my_custom_menu',plugins_url('assets/images/test.png', __FILE__));

    add_submenu_page( __FILE__ , 'My custom submenu-1', 'My custom submenu-1', 'manage_options', 'sub-suge', 'my_custom_submenu_1');
    add_submenu_page( __FILE__ , 'My custom submenu-2', 'My custom submenu-2', 'manage_options', 'sub-page', 'my_custom_submenu_2');

    add_action( 'admin_init', 'my_custom_menu' );
}

My problem is when i create submenu for the above top main menu then it gives a top menu link in the position of the submenu, that means it gives 3 submenu include main menu link. Please see the below link example output.

http://jaskokoyn.com/2013/03/20/wordpress-admin-submenus/

But i don't want main menu link into sub menu. I just want two Submenu under main menu what i have define in my code.

Can anyone give any better solution for this?

Thanks in advance.

回答1:

You can achieve this by giving the top-level page (the add_menu_page() call) the same slug as the first submenu page. Then reference this slug in the first parameter of both add_submenu_page() calls.

Here's the code:

add_menu_page( 
    'My custom menu Settings', 
    'My custom menu Settings', 
    'manage_options', 
    'sub-suge', 
    'my_custom_menu', 
    plugins_url( 'assets/images/test.png', __FILE__ ) 
);

add_submenu_page( 
    'sub-suge' , 
    'My custom submenu-1', 
    'My custom submenu-1', 
    'manage_options', 
    'sub-suge', 
    'my_custom_submenu_1' 
);

add_submenu_page( 
    'sub-suge' , 
    'My custom submenu-2', 
    'My custom submenu-2', 
    'manage_options', 
    'sub-page', 
    'my_custom_submenu_2' 
);


标签: php wordpress