另一个答案是这方面的简短版本。然而,我做了一个函数,它做得更多。它是这样做的:
它获取位置(以便以后添加新菜单)
它获取任何名为“移动菜单”的菜单,并获取其ID它将“移动菜单”设置为激活,以显示所调用导航的显示位置nav-mobile
然后它就保存了一切<?php
add_action( \'init\', function() {
// Variables
$location_name = \'nav-mobile\'; // As it\'s named in register_nav_menus( ... )
$menu_name = \'Mobile Menu\'; // The name of the menu in the WP backend
$save_new_locations = false; // To only save, if it isn\'t set already
$locations = get_theme_mod( \'nav_menu_locations\' ); // Get all nav_locations
if( ! isset( $locations[ $location_name ] ) ){
$menus = get_terms( \'nav_menu\' );
$menus = array_combine( wp_list_pluck( $menus, \'term_id\' ), wp_list_pluck( $menus, \'name\' ) );
// Above-written line matches id to menu name, like this:
// [
// 4129 => \'my-navigation\',
// 4123 => \'my-other-navigation\',
// ...
// ]
// where 4129 and 4123 is the id of the menus
$mobile_menu_key = array_search( $menu_name, $menus ); // Finds ID
if( !empty( $mobile_menu_key ) ){
$locations[ $location_name ] = $mobile_menu_key;
$save_new_locations = true;
}
}
if( $save_new_locations ){
set_theme_mod( \'nav_menu_locations\', $locations );
}
});