您可以使用get_theme_mod
和set_theme_mod
保存导航菜单位置。
获取位置:
$locations = get_theme_mod( \'nav_menu_locations\' );
var_dump( $locations );
打印:
Array
(
[primary] => 0
[secondary] => 0
)
获取现有导航菜单:
$nav_menus = get_terms( \'nav_menu\', array( \'hide_empty\' => true ) );
var_dump( $nav_menus );
打印:
Array
(
[0] => stdClass Object
(
[term_id] => 2
[name] => Main
[slug] => main
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => nav_menu
[description] =>
[parent] => 0
[count] => 3
)
}
设置位置:
$nav_menu_id = $nav_menus[0]->term_id;
$location = \'primary\';
$locations[ $location ] = $nav_menu_id;
set_theme_mod( \'nav_menu_locations\', $locations );