如何在二十个主题中获得多个菜单?
在使用TwentyTen主题时创建多个菜单?
有several steps to creating additional menus using the new menu system in WordPress 在二十十个主题或任何WordPress主题中(其中一些步骤可以无序完成)。此外,我强烈建议您创建一个基于二十个十的子主题,而不是直接修改它:
- Create a Child Theme 基于210主题。
- Register a Theme Location 在您的主题中
functions.php
文件 - Create a New Menu 使用网站的管理控制台。
- Associate the New Menu and the Theme Location 使用管理控制台。
- Call
wp_nav_menu()
in the Template File 要在其中显示菜单的主题。 - Style your Menu 以便它与您的站点进行视觉集成。
所以让我们继续吧!
1。创建子主题创建a child theme is extremely simple 如果新版本发布,您可以升级二十个,而不用担心丢失更改。
是的,您的更改很可能与新版本不兼容,如果您复制了二十个文件并为您的子主题修改它们,您将需要重新应用这些更改,但这比升级主题时放弃更改要好得多
但我不会重复我在另一个问题中提出的儿童主题的答案,我只会告诉你:
- Customizing a WordPress theme without changing it? (Use Child Themes)
2。注册主题位置is very straightforward using the
register_nav_menus()
function (是的,如果他们调用了该函数,那就太好了register_nav_menu_locations()
, 但我离题了……)我将创建一个"Footer" 此示例的菜单。
在我的示例中,请注意我如何引用
\'primary\'
在评论中;我这样做是为了让您看到默认导航菜单位置的名称,您不必自己定义它。还请注意,我过去__()
翻译函数,并将子主题的名称指定为翻译域。如果您使用的是子主题,则需要创建
functions.php
在您的主题中创建文件以容纳此功能,但如果您要修改主题,只需查找functions.php
并将其添加到末尾:
如果您正在为自己的主题编写代码,并且不需要将其分发给其他人或担心翻译,那么您可以这样做:register_nav_menus(array( //\'primary\' => __(\'Primary Menu Area\',\'himanshu\'), ==> Primary defined by default \'footer\' => __(\'Footer Menu Area\',\'himanshu\'), ));
register_nav_menus(array(\'footer\'=>\'Footer Menu Area\'));
3。创建一个新菜单接下来,让我们通过导航到Menus 的选项Appearance 管理控制台中的菜单。单击“+”添加菜单,键入菜单名称,然后单击“创建菜单”:
(来源:mikeschinkel.com)Note 您通常会将菜单的名称与菜单位置的名称相同,但这不是必需的,并且WordPress treats menus and their menu locations them as separate entities.
一定要add some options to your menu 否则就没什么用了。使用管理控制台选择所需的选项,将其添加到菜单中,然后保存(在我的屏幕截图中,我只显示为菜单选项选择“页面”,但您可以混合和匹配WordPress提供的任何类型的菜单选项):
(来源:mikeschinkel.com)4。将新菜单和主题位置关联起来很容易,只需使用WordPress的管理控制台:
(来源:mikeschinkel.com)5。呼叫
wp_nav_menu()
在模板文件中,现在我们需要返回到代码。我复制了一份footer.php
并将其复制到“Himanshu”主题目录中。以下是前18行的样子:
我把电话插入到<?php /** * The template for displaying the footer. * * Contains the closing of the id=main div and all content * after. Calls sidebar-footer.php for bottom widgets. * * @package WordPress * @subpackage Himanshu (based on Twenty Ten) * @since Twenty Ten 1.0 */ ?> </div><!-- #main --> <div id="footer" role="contentinfo"> <div id="colophon">
wp_nav_menu()
以及包装器HTML<div id="colophon">
在第18行,因此第13到24行现在如下所示:
Note 我选择调用包装器</div><!-- #main --> <div id="footer" role="contentinfo"> <div id="colophon"> <div id="footernav" role="navigation"> <?php wp_nav_menu(array( \'container_class\' => \'menu-footer\', \'theme_location\' => \'footer\' )); ?> </div>
footernav
和内部容器menu-footer
我跟着二万的线索role="navigation"
. 然而the most important aspect of the code is\'theme_location\' => \'footer\'
与步骤#2中的命名主题位置相匹配。所有这些步骤都为我们提供了一个页脚菜单,如下所示:
(来源:mikeschinkel.com)6。最后,我们只需要在主题中添加CSS
style.css
文件,我们可以得到一个页脚菜单,如下所示:
(来源:mikeschinkel.com)造型非常基础,所以请不要像我这样拿我糟糕的设计技巧来对抗我not 我从来没有威胁过要成为一名设计师我在CSS代码中加入了注释,解释了为什么我使用了每个选择器和CSS属性:
就是这样!请注意,这是一个供设计师使用的工具,因此您或无论您的设计师是谁,都可以从主题的角度以您喜欢的任何方式实现菜单;打电话给#colophon { padding-top:6px; /* Move menu closer to thick black line (TwentyTen has 18px) */ } #footernav { /* Use same font-family as TwentyTen does for menus */ font-family: \'Helvetica Neue\', Arial, Helvetica, \'Nimbus Sans L\', sans-serif; font-size:1.1em; /* Make a little bigger than default */ padding-bottom:6px; /* Put some breathing room under the menu */ } #footernav .menu-footer { text-align:center; /* Needed to center the menu */ } #footernav ul { margin:0 auto; /* Also needed to center the menu */ width:auto; /* Make menu only as wide as needs to be */ display:inline; /* Also needed to keep width to a minumum */ } #footernav li { display:inline; /* Make menu horizontal instead of veritcal */ } #footernav a { text-decoration:none; /* Remove underlines from links */ background-color:#ddd; /* Create a light grey background for each option */ color:black; /* Make the items easy to see with text in black */ padding:0.25em 0.5em; /* Add space around the items for the background to display*/ margin:0 0.5em; /* Add space between the items */ } #footernav a:hover { background-color:black; /* Surround the menu item under the mouse pointer in black */ color:white; /* Make the text for the same menu item be white */ }
wp_nav_menu()
函数引用您的菜单和菜单位置,您就可以开始了!
- Customizing a WordPress theme without changing it? (Use Child Themes)