2

I am creating pages and assign them to a menu with custom link. Pages are properly created and menu is properly created, too. However, custom links are not given to the menu items.

What I want is to assign a custom link # (so that the link doesn't work) to a menu item, but no success yet.

What am I doing wrong here?

My code (simplified)

  1. Creating a page

    $target_page[0] = array( 'slug' => 'home',
                         'title' => 'Home',
                         'post_excerpt' => 'Home',
                         'menu_order' => 0);
    
    $target_page[1] = array( 'slug' => 'sales',
                         'title' => 'Sales',
                         'post_excerpt' => 'Sales',
                         'menu_order' => 1 );
    
    $cnt_target_pages = sizeof( $target_page );
    
    for( $i = 0; $i < $cnt_target_pages; $i++ ) {
        $page_found = 0;
        $pages = get_pages( array( 'post_status' => 'publish' ) );
        foreach( $pages as $page ) {
            $slug = $page->post_name;
            if( $slug === $target_page[$i]['slug'] ) $page_found = 1;
        }
        if( $page_found == 0 ) {
            $page_id = wp_insert_post( array(
                'post_title'    => $target_page[$i]['title'],
                'post_type'     => 'page',
                'post_name'     => $target_page[$i]['slug'],
                'post_status'   => 'publish',
                'post_excerpt'  => $target_page[$i]['post_excerpt'],
                'comment_status'=> 'closed',
                'menu_order'    => $target_page[$i]['menu_order'],
            ));
        }
    }
    
  2. Creating a menu

    $menu_name = 'my_menu';
    $menu_id = wp_create_nav_menu( $menu_name );
    $locations = get_theme_mod('nav_menu_locations');
    $locations['primary'] = $menu_id;
    set_theme_mod('nav_menu_locations', $locations);
    
    $new_menu_obj = array();
    
    $nav_items_to_add = array(
                'home' => array( 'title' => 'Home',
                                 'path' => 'home', ),                   
                'sales' => array( 'title' => 'Sales',
                                  'path' => 'sales',
                                  'menu-item-url' => '#'));
    
    foreach ( $nav_items_to_add as $slug => $nav_item ) {
    
            $new_menu_obj[$slug] = array();
    
            $new_menu_obj[$slug]['id'] = wp_update_nav_menu_item($menu_id, 0,  array(
                    'menu-item-title'     => $nav_item['title'],
                    'menu-item-object'    => 'page',
                    'menu-item-parent-id' => $new_menu_obj[$nav_item['parent']]['id'],
                    'menu-item-url'       => $nav_item['menu-item-url'], // This doesn't work. 'menu-item'url' => '#' doesn't work, either.
                    'menu-item-type'      => 'post_type',
                    'menu-item-status'    => 'publish')
            );
        }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.