我正在尝试将BuddyPress导航菜单支持添加到我的主题中,不幸的是,BP的模板标签仍然没有完全达到标准。(基本上,如果您没有为BP默认主题创建明确的子主题,则必须重新设计几个轮子。)
所以我想做的是
检测BP何时处于活动状态(我知道怎么做)
在BP处于活动状态时注册菜单位置(也是已知数量)创建包含BP部分链接的默认菜单(this is where the hole in my knowledge exists)将所述默认菜单分配给新注册的位置,因此,本质上,在我的主题处于活动状态的情况下,如果用户激活BuddyPress,他们将自动获得包含成员、论坛、活动等的菜单,并将其显示到某个位置,but 如果用户想要覆盖菜单,他们可以自由地这样做。思想?EDIT 1
贝因特获奖。以下是我所做的,与他的解决方案略有不同:我有条件地注册了一个菜单位置
if( function_exists( \'bp_get_loggedin_user_nav\' ) ){
register_nav_menu( \'lblgbpmenu\', \'Default BuddyPress Menu\' );
}
然后,我有条件地挂接了对菜单设置的调用if( function_exists( \'bp_get_loggedin_user_nav\' ) ){
add_action( \'widgets_init\', \'lblg_add_default_buddypress_menu\' );
}
最后,我注册了菜单function lblg_add_default_buddypress_menu(){
global $lblg_themename;
$menuname = $lblg_themename . \' BuddyPress Menu\';
$bpmenulocation = \'lblgbpmenu\';
// Does the menu exist already?
$menu_exists = wp_get_nav_menu_object( $menuname );
// If it doesn\'t exist, let\'s create it.
if( !$menu_exists){
$menu_id = wp_create_nav_menu($menuname);
// Set up default BuddyPress links and add them to the menu.
wp_update_nav_menu_item($menu_id, 0, array(
\'menu-item-title\' => __(\'Home\'),
\'menu-item-classes\' => \'home\',
\'menu-item-url\' => home_url( \'/\' ),
\'menu-item-status\' => \'publish\'));
wp_update_nav_menu_item($menu_id, 0, array(
\'menu-item-title\' => __(\'Activity\'),
\'menu-item-classes\' => \'activity\',
\'menu-item-url\' => home_url( \'/activity/\' ),
\'menu-item-status\' => \'publish\'));
wp_update_nav_menu_item($menu_id, 0, array(
\'menu-item-title\' => __(\'Members\'),
\'menu-item-classes\' => \'members\',
\'menu-item-url\' => home_url( \'/members/\' ),
\'menu-item-status\' => \'publish\'));
wp_update_nav_menu_item($menu_id, 0, array(
\'menu-item-title\' => __(\'Groups\'),
\'menu-item-classes\' => \'groups\',
\'menu-item-url\' => home_url( \'/groups/\' ),
\'menu-item-status\' => \'publish\'));
wp_update_nav_menu_item($menu_id, 0, array(
\'menu-item-title\' => __(\'Forums\'),
\'menu-item-classes\' => \'forums\',
\'menu-item-url\' => home_url( \'/forums/\' ),
\'menu-item-status\' => \'publish\'));
// Grab the theme locations and assign our newly-created menu
// to the BuddyPress menu location.
if( !has_nav_menu( $bpmenulocation ) ){
$locations = get_theme_mod(\'nav_menu_locations\');
$locations[$bpmenulocation] = $menu_id;
set_theme_mod( \'nav_menu_locations\', $locations );
}
}
}
最合适的回答,由SO网友:Bainternet 整理而成
因此,基本上您要问的是如何通过代码创建自定义菜单并将其分配到菜单位置:
//give your menu a name
$name = \'theme default menu\';
//create the menu
$menu_id = wp_create_nav_menu($name);
//then get the menu object by its name
$menu = get_term_by( \'name\', $name, \'nav_menu\' );
//then add the actuall link/ menu item and you do this for each item you want to add
wp_update_nav_menu_item($menu->term_id, 0, array(
\'menu-item-title\' => __(\'Home\'),
\'menu-item-classes\' => \'home\',
\'menu-item-url\' => home_url( \'/\' ),
\'menu-item-status\' => \'publish\'));
// you add as many items ass you need with wp_update_nav_menu_item()
//then you set the wanted theme location
$locations = get_theme_mod(\'nav_menu_locations\');
$locations[\'LOCATION_NAME\'] = $menu->term_id;
set_theme_mod( \'nav_menu_locations\', $locations );
所以你所要做的就是添加任意多的链接,更改
LOCATION_NAME
并确保此代码只运行一次。