Create subpages in default WordPress menu

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.

Related posts

2 comments

  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.

Comments are closed.