get_registered_nav_menus()
云策文档标注
概述
get_registered_nav_menus() 函数用于检索主题中所有已注册的导航菜单位置。它返回一个关联数组,键为菜单位置,值为描述;若无注册位置,则返回空数组。
关键要点
- 函数返回 string[] 类型的关联数组,键是菜单位置,值是描述
- 如果未注册任何菜单位置,返回空数组
- 函数内部使用全局变量 $_wp_registered_nav_menus 来获取数据
- 该函数自 WordPress 3.0.0 版本引入
代码示例
$menus = get_registered_nav_menus();
if ( ! empty( $menus ) ) {
foreach ( $menus as $location => $description ) {
echo $location . ': ' . $description . '<br>';
}
}注意事项
- 函数仅检索当前活动主题中注册的菜单位置,不适用于其他主题
- 返回的数组示例可能包含如 ['primary-menu' => 'Primary Menu', 'secondary-menu' => 'Secondary Menu', 'footer-menu' => 'Footer Menu'] 的键值对
原文内容
Retrieves all registered navigation menu locations in a theme.
Source
function get_registered_nav_menus() {
global $_wp_registered_nav_menus;
if ( isset( $_wp_registered_nav_menus ) ) {
return $_wp_registered_nav_menus;
}
return array();
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 4 content
Codex
Display a simple list of the menus
$description ) { echo $location . ': ' . $description . '<br />'; }Skip to note 5 content
trex005
I think this should say “Returns an array of all registered navigation menus in active theme”, as current wording sounds like you can use this function on other themes (which is what I’m currently looking for)
Skip to note 6 content
Philipp Stracker
Sample return value: