php 如何在 WordPress 模板中插入第二个菜单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10635880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert a second menu into a WordPress template?
提问by Cecil Rodriguez
So I'm trying to add a second menu to a WordPress template - the first I've got by writing the following:
因此,我正在尝试向 WordPress 模板添加第二个菜单 - 我通过编写以下内容获得的第一个菜单:
<?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?>
Now, I've got two menus registered in the functions.phpfile, as follows:
现在,我在functions.php文件中注册了两个菜单,如下所示:
register_nav_menu('header', 'Header Menu');
register_nav_menu('ad-menu1', 'Ad Menu One');
How do I access whatever menu is in the second nav menu registered? Or am I registering incorrectly? I've tried:
如何访问注册的第二个导航菜单中的任何菜单?还是我注册错了?我试过了:
<?php wp_nav_menu( array( 'theme_location' => 'ad-menu1', 'container_class' => 'menu-ads' ) ); ?>
But that only gives me a list of every category, which is NOT what I want.
但这仅给了我每个类别的列表,这不是我想要的。
How do I merely grab the menu that is associated with Ad Menu One/ad-menu1?
我如何只抓取与广告菜单一/广告菜单1关联的菜单?
回答by inigomedina
Standard way of adding a secondary menu to a theme is as follows.
向主题添加二级菜单的标准方法如下。
Add the function to create a new menu opening file functions.phpand registeringit:
添加函数来创建一个新的菜单打开文件functions.php,registering它:
register_nav_menus( array(
'primary' => __( 'Primary Menu', 'yourtheme'),
'secondary' => __( 'Secondary Menu', 'yourtheme' ),
) );
This brought up a 2nd menu in the Theme Menu options.
这带来了主题菜单选项中的第二个菜单。
Next, add the code to the desired place on your theme file. In this case, it would be added to the footer.
接下来,将代码添加到主题文件的所需位置。在这种情况下,它将被添加到页脚。
<nav>
<?php
wp_nav_menu( array('container_class' => 'menu-footer',
'theme_location' => 'secondary') ); ?>
</nav>

