WP_UPDATE_NAV_MENU_ITEM()以插入类别

时间:2014-02-05 作者:N2Mystic

在下面的脚本中,我尝试将类别动态添加到自定义菜单中。除了类别没有超链接之外,一切都很好。下面的wp\\u update\\u nav\\u menu\\u item()中缺少什么?

        $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 个回复
最合适的回答,由SO网友:N2Mystic 整理而成

使用“外观>菜单”类别面板上的Chrome检查器后,我能够嗅出通过向导手动将类别添加到自定义菜单时传递的隐藏表单值:

<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>
因此,生成的脚本变成:

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

SO网友:Jim VanPetten

我想知道它是否需要对象id:

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

SO网友:mneil

在插入类别的菜单项的地方,数组不像插入主链接那样包含“菜单项url”键。

    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)
            );
        }
    }
存在已更正的foreach循环

结束

相关推荐

Style wp_list_categories

我认为这是一个相当简单的问题,如果是,我很抱歉,但我如何添加<div> 此代码中的每个单独类别: <?php $taxonomy = \'category\'; // get the term IDs assigned to post. $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( \'fields\' => \'ids\' ) ); // separato