11

目前我正在制作这个菜单:(Functions.php)

$menuname = 'Top Menu';
$menu_exists = wp_get_nav_menu_object( $menuname );

if( !$menu_exists){
$menu_id = wp_create_nav_menu($menuname);

wp_update_nav_menu_item($menu_id, 0, array(
    'menu-item-title' =>  __('Programme'),
    'menu-item-classes' => 'programme',
    'menu-item-url' => home_url( '/programme/' ), 
    'menu-item-status' => 'publish'));

wp_update_nav_menu_item($menu_id, 0, array(
    'menu-item-title' =>  __('Speakers'),
    'menu-item-classes' => 'speakers',
    'menu-item-url' => home_url( '/speakers/' ), 
    'menu-item-status' => 'publish'));
}

当我激活我的主题时,我想做的是:( 来源:cubeupload.com激活

自动选择“主菜单”框,因此当我启动此主题时,我会创建一个菜单并将其设为主菜单。

如何做到这一点?

4

1 回答 1

17

您可以通过以下theme_location方式以编程方式设置菜单:

$locations = get_theme_mod('nav_menu_locations');
$locations['primary-menu'] = $term_id_of_menu;
set_theme_mod( 'nav_menu_locations', $locations );

将此添加到您的functions.php.

于 2013-10-28T14:45:15.700 回答