1

I've got this code below working to create a default menu. I was wondering how I would create sub-items/subpages...

// Check if the menu exists
$menu_exists = wp_get_nav_menu_object('Main Menu');

// If it doesn't exist, let's create it.
if( !$menu_exists){
    $menu_id = wp_create_nav_menu('Main Menu');


  // Set up default menu items

    wp_update_nav_menu_item($menu_id, 0, array(
        'menu-item-title' =>  __('Home'),
        'menu-item-type' => 'post_type',
        'menu-item-object' => 'page',
        'menu-item-url' => get_permalink($pageId),
        'menu-item-status' => 'publish'));
}

[edit] Sorry all! I forgot to mention that I was implementing this menu on every installation of a Wordpress multisite. So when you do a new installation this default menu would already be in place.

4
  • 1
    What happens when you use the Custom Nav Menu feature as it's intended, instead of trying to force a square peg into a round hole? Oct 21, 2013 at 15:12
  • @ChipBennett Sorry, I was hoping to this on a Wordpress Multisite installation.
    – Laura
    Oct 23, 2013 at 2:54
  • What does the use of Multisite have to do with wp_nav_menu()? Oct 23, 2013 at 11:32
  • Whenever you do a new installation of a multisite, the theme comes with a pre-existing menu
    – Laura
    Oct 23, 2013 at 22:19

2 Answers 2

0

I was also doing the same thing check out this question Hope this will help you .

Menu page Exists or not

-1

Are you using one of the default themes or something similar? You normally organise sub-pages by setting the 'Parent' attribute in the 'Edit Page' page. You'll probably find that the default menu will then show these correctly.

Alternatively, you can always edit the structure and order of a menu through the WP dashboard...Appearance...Menus. You drag and drop pages or categories into the menu as required. If you drag a page to the right inside the structure box, it will make it a sub-item in the menu.

2
  • Sorry, I was hoping to this on a Wordpress Multisite installation.
    – Laura
    Oct 23, 2013 at 2:53
  • Apologies, I didn't properly look at what you were doing!
    – cfgm
    Oct 26, 2013 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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