wp_update_nav_menu_item() to insert categories

In the script below, I’m attempting to dynamically add categories to a custom menu. Everything works great, except for the fact that the categories do not have hyperlinks. What am I missing in wp_update_nav_menu_item() below?

        $cat_args=array(
        'hierarchical' => 0,
        'exclude' => '1',
        'exclude_tree' => '1',
        'hide_empty' => 1,
        );

        $theCats = get_categories($cat_args);

        $name = 'Site Menu';
        $menu_id = wp_create_nav_menu($name);
        $menu = get_term_by( 'name', $name, 'nav_menu' );

        /* insert the a link to home */
        wp_update_nav_menu_item($menu->term_id, 0, array(
            'menu-item-title' => 'Home',
            'menu-item-url' => get_bloginfo('url'),
            'menu-item-status' => 'publish')
        );

        /* insert each category except uncategorized */
        if (count($theCats) > 0){
            foreach($theCats as $category){
                wp_update_nav_menu_item($menu->term_id, 0, array(
                    'menu-item-title' => $category->name,
                    'menu-item-type' => 'taxonomy',
                    'menu-item-status' => 'publish',
                    'menu-item-object' => 'category',
                    'menu-item-parent-id' => 0)
                );
            }
        }

        theme_set_nav_menu($menu->term_id,'header-menu');

Related posts

3 comments

  1. After using the Chrome inspector on the “Appearance > Menus” categories panel I was able to sniff out the hidden form values that are passed when one manually adds a category to a custom menu via the wizard:

    <li>
        <label class="menu-item-title">
            <input type="checkbox" class="menu-item-checkbox" name="menu-item[-11][menu-item-object-id]" value="181"> Category One</label>
            <input type="hidden" class="menu-item-db-id" name="menu-item[-11][menu-item-db-id]" value="0">
            <input type="hidden" class="menu-item-object" name="menu-item[-11][menu-item-object]" value="category">
            <input type="hidden" class="menu-item-parent-id" name="menu-item[-11][menu-item-parent-id]" value="0">
            <input type="hidden" class="menu-item-type" name="menu-item[-11][menu-item-type]" value="taxonomy">
            <input type="hidden" class="menu-item-title" name="menu-item[-11][menu-item-title]" value="Category One">
            <input type="hidden" class="menu-item-url" name="menu-item[-11][menu-item-url]" value="http://localhost:8888/silo4/./category-one/">
            <input type="hidden" class="menu-item-target" name="menu-item[-11][menu-item-target]" value="">
            <input type="hidden" class="menu-item-attr_title" name="menu-item[-11][menu-item-attr_title]" value="">
            <input type="hidden" class="menu-item-classes" name="menu-item[-11][menu-item-classes]" value="">
            <input type="hidden" class="menu-item-xfn" name="menu-item[-11][menu-item-xfn]" value="">
        </li>
    

    So, the resulting script becomes:

    wp_update_nav_menu_item($menu->term_id, 0, array(
        'menu-item-title' => $category->name,
        'menu-item-object-id' => $category->term_id,
        'menu-item-db-id' => 0,
        'menu-item-object' => 'category',
        'menu-item-parent-id' => 0,
        'menu-item-type' => 'taxonomy',
        'menu-item-url' => get_category_link($category->term_id),
        'menu-item-status' => 'publish',)
    );
    
  2. Well where you insert the menu items for the categories, your array does not contain the ‘menu-item-url’ key like it does for inserting your home link.

        if (count($theCats) > 0){
            foreach($theCats as $category){
                wp_update_nav_menu_item($menu->term_id, 0, array(
                    'menu-item-title' => $category->name,
                    'menu-item-url' => get_category_link($category->term_id),
                    'menu-item-type' => 'taxonomy',
                    'menu-item-status' => 'publish',
                    'menu-item-object' => 'category',
                    'menu-item-parent-id' => 0)
                );
            }
        }
    

    There is the corrected foreach loop

Comments are closed.