register_nav_menus()
最后更新于:2021-11-27 21:35:31
register_nav_menus( string[]$locations=array())Registers navigation menu locations for a theme.
参数
- $locations
-
(string[]) (Optional) Associative array of menu location identifiers (like a slug) and descriptive text.
Default value: array()
源文件
文件: gc-includes/nav-menu.php
function register_nav_menus( $locations = array() ) {
global $_gc_registered_nav_menus;
add_theme_support( 'menus' );
foreach ( $locations as $key => $value ) {
if ( is_int( $key ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Nav menu locations must be strings.' ), '5.3.0' );
break;
}
}
$_gc_registered_nav_menus = array_merge( (array) $_gc_registered_nav_menus, $locations );
}
function cebbi_update_menu_location() { // Get assigned menus from theme mods $nav_menu_locations = get_theme_mod('nav_menu_locations'); // Check if old-slug was previously assigned if (isset($nav_menu_locations['old-slug'])) { // Verify that new location does not yet exist or is empty to avoid overwriting manual changes if (!isset($nav_menu_locations['new-slug']) || $nav_menu_locations['new-slug'] === 0) { // Copy assigned menu index to new location $nav_menu_locations['new-slug'] = $nav_menu_locations['old-slug']; // Optional: delete the menu assignment to the old location unset($nav_menu_locations['old-slug']); // Update theme mod set_theme_mod('nav_menu_locations', $nav_menu_locations); } } } add_action('after_setup_theme', 'cebbi_update_menu_location');
$tax = 'country-category'; $terms = get_terms( $tax, [ 'hide_empty' => false, ]); $args = array( 'primary' => __( 'Primary Menu', 'khoipro' ), 'secondary' => __( 'Secondary Menu', 'khoipro' ) ); // Loop through all terms to add term id as menu id and term name as menu name. foreach( $terms as $term) { $args = array_merge($args, array( 'primary_'.$term->slug => 'Country Menu ('.$term->name.')' )); } register_nav_menus( $args );