2

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');
3

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',)
);
0

I wonder if it needs object id:

'menu-item-object-id' => $category->cat_ID,
-1

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

  • Your code does not work. The hyperlink is still blank. It might work if the item-type were "custom", but not with Taxonomy. – N2Mystic Feb 5 '14 at 5:22
  • I've been chasing this for a couple days on and off. I think N2Mystic is right. I just looked at the function and see this: if ( 'custom' != $args['menu-item-type'] ) { /* if non-custom menu item, then: * use original object's URL * blank default title to sync with original object's */ $args['menu-item-url'] = ''; – Jim VanPetten Dec 13 '18 at 21:32

Your Answer

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

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