在主题激活时自动在位置“主菜单”上设置菜单

时间:2013-10-16 10:54:13

标签: wordpress menu themes activation

目前我正在制作此菜单: (的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'));
}

当我激活我的主题时,我要做的是: Activating

选择'主菜单'自动框,所以当我开始这个主题时,我创建一个菜单并使其成为主菜单。

如何做到这一点?

1 个答案:

答案 0 :(得分:13)

您可以使用以下方式以编程方式设置菜单的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