为自定义 Wordpress 主题添加菜单支持 [英] Adding Menu Support to Custom Wordpress Theme
问题描述
我正在创建我的第一个 Wordpress 主题,但在菜单支持方面遇到了困难:
I am creating my first Wordpress theme and I am struggling with menu support:
我在functions.php中添加了一个自定义菜单并将其实现到我的header.php中,如下所示,但是管理区域中的菜单选项没有显示!
# functions.php
<?php
add_theme_support( 'menus' );
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu' ),
'secondary-menu' => __( 'Secondary Menu' )
)
);
}
?>
# header.php
# [...]
<?php wp_nav_menu( array( 'theme_location' => 'primary-menu' ) ); ?>
# [...]
我的设置:
- Wordpress 3.4.2 版
- MAMP 开发环境
- 没有插件
其他信息:
- 菜单选项显示在其他模板中
- 菜单在页面上正确呈现
我在这里遗漏了什么?
我什至看不到管理菜单中的菜单选项(就像这里!)
I can't even see the menu option in the admin menu (like here!)
推荐答案
很少有事情 - 你不需要 add_theme_support();也不是 add_action('init', 'register_my_menus')
Few things - You don't need add_theme_support(); nor the add_action('init', 'register_my_menus')
直接调用 register_nav_menus 函数,如下所示:
Just straight up call the register_nav_menus function, like so:
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu' ),
'secondary-menu' => __( 'Secondary Menu' )
)
);
如果您愿意,也可以检查该功能是否存在.但是,如果它仅用于您自己的主题,并且您知道它存在,那么它并不是真正需要的.
Can also check if the function exists if you desire. But if it's only for use on your own theme and you know it exists it's not really needed.
if ( function_exists( 'register_nav_menus' ) ) {
...
}
这篇关于为自定义 Wordpress 主题添加菜单支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!