Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using this code:

add_menu_page($page_title, $menu_title, $this->capability, $menu_slug, $function);

Which is adding top level admin page. When I add:

add_submenu_page( $menu_slug, 'sub menu 1', 'sub menu 1', $this->capability, $menu_slug . '_sub_menu_page_1', $function );

I get not only the desired child page, but also the parent page moves to become a child page of itself.

share|improve this question
    
You could try unsetting it from the $submenu var inside a callback hooked onto admin_menu. –  t31os Nov 23 '11 at 9:57
    
how have you set $menu_slug? –  rds Nov 25 '11 at 9:07
    
menu_slug and menu_slug_sub_menu_1 –  Mild Fuzz Dec 1 '11 at 19:52
    
Can we see the actual code being used? –  Chip Bennett Dec 2 '11 at 0:54

3 Answers 3

up vote 2 down vote accepted
+50

Unless I misunderstand the question, this should do the trick:

add_action('admin_menu', 'add_menu_pages');

function add_menu_pages() {
    add_menu_page('Menu Title', 'Menu Title', 10, 'main_menu', 'submenu_1_callback');
    add_submenu_page('main_menu', 'Sub Menu Title 1', 'Sub Menu Title 1', 10,  'main_menu' , 'submenu_1_callback');
    add_submenu_page('main_menu', 'Sub Menu Title 2', 'Sub Menu Title 2', 10, 'sub_menu' , 'submenu_2_callback');
}

function submenu_1_callback() {
    echo "<h2>" . __( 'Toplevel & Sublevel 1', 'menu-test' ) . "</h2>";
}

function submenu_2_callback() {
    echo "<h2>" . __( 'Sublevel 2', 'menu-test' ) . "</h2>";
}

EDIT:

Here's the easiest way to hide the first sub-menu item.

1) Create admin.css and move it to /[your template dir]/css/

2) Add this to your admin.css:

#adminmenu li#toplevel_page_main_menu li.wp-first-item {
    display: none !important;
}

3) Add the following code to your functions.php

add_action('admin_enqueue_scripts', 'custom_admin_css');
function custom_admin_css() {
    wp_enqueue_style( 'admin_css', (get_template_directory_uri() . '/css/admin.css'), false, '1.0.0' );
}
share|improve this answer
    
no, the problem is the way it displayed on the sidebar. I do not want the main page to be repeated in the sub pages, beneath it. –  Mild Fuzz Dec 1 '11 at 19:50
    
Edited answer should work. You should realize, however, that when the admin menu is collapsed and you hover over the top-level menu icon, the top-level menu item text becomes a label instead of a link (only the icon stays a link). –  Paul Dec 2 '11 at 0:46

Is there some reason that you don't just use remove_submenu_page() as applicable to remove the page being added, and then just build (or re-build, as the case may be) your menu structure?

But really, that would be a hack, and the need to use a hack is indicative of something doing wrong that needs to be fixed. For that, we'll probably need the actual code being used.

share|improve this answer

The standard trick is to repeat the main menu page as a submenu page, without the menu title (3rd parameter).

function add_menu_pages() {
    add_menu_page('Page Title', 'Menu Title', 'edit_posts', 'main_menu', 'menu_1_callback');
    add_submenu_page('main_menu', 'Page Title', '', 'edit_posts',  'main_menu' , 'menu_1_callback');
    add_submenu_page('main_menu', 'Page Title 1', 'Submenu Title 1', 'edit_posts',  'main_menu' , 'submenu_1_callback');
    add_submenu_page('main_menu', 'Page Title 2', 'Submenu Title 2', 'edit_posts', 'sub_menu' , 'submenu_2_callback');
}

That will give you

Menu Title
Submenu Title 1
Submenu Title 2
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.