$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>theme development - wp_update_nav_menu_item() to insert categories|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

theme development - wp_update_nav_menu_item() to insert categories

matteradmin8PV0评论

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

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');
Share Improve this question asked Feb 5, 2014 at 1:21 N2MysticN2Mystic 3,1937 gold badges47 silver badges72 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 4

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

I wonder if it needs object id:

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

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

Post a comment

comment list (0)

  1. No comments so far