如何编程添加到新的WordPress菜单

时间:2010-09-26 作者:Brad

预WordPress 3。我使用的x:


add_action( \'init\', \'product_register\' );
add_filter( \'wp_page_menu\', \'custom_page_nav\', 90 );

function product_register()
{
    //    register_post_type( \'product\', $args );

    register_taxonomy( "catalog", array( "product" ), array( "hierarchical" => true, "label" => "Catalogs",
        "singular_label" => "Catalog", \'hierarchical\' => true, \'show_ui\' => true, \'show_in_nav_menus\' => true,
        \'show_tagcloud\' => true, \'query_var\' => true, "rewrite" => true, \'public\' => true, \'capability_type\' => \'post\' ) );
    //\'_builtin\' => false,\'_edit_link\' => \'post.php?post=%d\',
    ////////////////////////////////////////////////

}

function custom_page_nav( $menu )
{
    $taxonomy = \'catalog\';
    $orderby = \'name\';
    $show_count = 0; // 1 for yes, 0 for no
    $pad_counts = 1; // 1 for yes, 0 for no
    $hierarchical = true; // 1 for yes, 0 for no
    $title = \'\';

    $args = array( \'taxonomy\' => $taxonomy, \'orderby\' => $orderby, \'show_count\' => $show_count, \'pad_counts\' => $pad_counts,
        \'hierarchical\' => $hierarchical, \'title_li\' => false, \'echo\' => false, \'empty\' => true, \'hide_empty\' => true,
        \'depth\' => 0 );
    $links .= wp_list_categories( $args );
    //  $links .= wp_list_categories( array( \'title_li\' => false, \'echo\' => false, \'empty\' => true ) );
    $menu = str_replace( \'\', \'Prodcuts \' . $links . \'\', $menu );
    //
    return $menu;
}
如何使用新的WordPress 3做同样的事情。x菜单
使用此处的另一篇帖子,我能够找出如何创建新菜单,但没有向其中添加自定义分类项目:


function register_typenav()
{
    $mainnav = wp_get_nav_menu_object( \'Products\' );

    if ( ! $mainnav )
    {
        $menu_id = wp_create_nav_menu( \'Products\' );
        // vav item for each post type
        $types = get_post_types( array( \'exclude_from_search\' => false ), \'objects\' );

        foreach ( $types as $type )
        {
            if ( ! $type->_builtin )
            {
                wp_update_nav_menu_item( $menu_id, 0, array( \'menu-item-type\' => \'custom\', \'menu-item-title\' => $type->
                    labels->name, \'menu-item-url\' => get_bloginfo( \'url\' ) . \'/\' . $type->rewrite[\'slug\'] . \'/\',
                    \'menu-item-status\' => \'publish\' ) );
            }
        }
        if ( $mainnav && ! has_nav_menu( \'primary-menu\' ) )
        {
            $theme = get_current_theme();
            $mods = get_option( "mods_$theme" );
            $key = key( $mods[\'nav_menu_locations\'] );
            $mods[\'nav_menu_locations\'][$key] = $mainnav->term_id;
            update_option( "mods_$theme", $mods );
        }
    }
}
add_action( \'init\', \'register_typenav\' );

1 个回复
SO网友:sorich87

您可以使用过滤器“wp\\u nav\\u menu items”和“wp\\u nav\\u menu{$menu->slug}\\u items”。请参阅“wp includes/nav菜单模板”。php’,第222行。

结束

相关推荐