WordPress 给导航栏添加非菜单栏目

从WordPress3.0开始已集成菜单管理功能,使得创建和管理(导航)菜单变得轻而易举。现在,创建并显示一个菜单需要的无非就是一行代码( wp_nav_menu ),似乎已经失去“手动”添加我们自己需要的东西空间。 例如,菜单功能默认没有“返回首页”的任何链接,虽然可以很容易地在自定义菜单功能中手动添加一个返回首页的链接,但返回首页链接基本是网站必须的功能,因此,自动添加此功能是很有必要的。有一个更简单的方法,使用WordPress filters.

利用导航菜单“ filters”功能,可以使我们能够加入特定菜单项。

举一反三,经过挖掘研究,将以下三段代码,添加到主题 functions.php 文件中,会实现自动增加一个登录/注销链接、添加一个搜索框和一个返回首页的链接到你的WordPress3.0导航菜单。

一、新增一个登录/注销链接到您的导航菜单

  1. add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
  2. function add_login_logout_link($items$args) {
  3. ob_start();
  4. wp_loginout('index.php');
  5. $loginoutlink = ob_get_contents();
  6. ob_end_clean();
  7. $items .= '<li>'. $loginoutlink .'</li>';
  8. return $items;
  9. }

说明:

First we add a function add_login_logout_link to the wp_nav_menu_items filter. Then, the ob_start, ob_get_contents and ob_end_clean (lines 4, 6 and 7) functions are “output Buffering” PHP functions that will “intercept” the information that would otherwise be sent to the browser. wp_loginout('index.php'); will add the logic and html code to login (if not logged in yet), or logout (if logged in). Since we don’t want to send that code to the browser yet, we “capture” the output (using ob_get_contents) in a variable ($searchform), and finally include that variable as a list item in the menu.
二、为导航菜单自动添加搜索框

  1. add_filter('wp_nav_menu_items','add_search_box', 10, 2);
  2. function add_search_box($items$args) {
  3. ob_start();
  4. get_search_form();
  5. $searchform = ob_get_contents();
  6. ob_end_clean();
  7. $items .= '<li>' . $searchform . '</li>';
  8. return $items;
  9. }

创建自己的搜索模板
add_search_box是利用默认的searchform菜单栏。但是这可能不是理想的布局(也许它包含前文本"搜索:”与“搜索”键),那么你就应该创建一个模板文件searchform.php在你的主题模板目录,加入下面代码: 该 add_search_box 功能是利用默认的searchform模板。 但这未必是理想的布局(也许它包含前面的文本“搜索:”和一个“搜索”按钮),所以你可创建一个自己的searchform.php模板文件 ,放到在你的主题模板目录中,并添加以下代码:

  1. <form method="get" class="search-form" id="search-form" action="<?php bloginfo( 'home' ); ?>/">
  2. <div class="formfield">
  3. <input class="formInputText" type="text" name="s" id="search-text" value="Search ..." size="12"maxlength="16" tabindex="1" onfocus="if (this.value == 'Search ...') {this.value = '';}"onblur="if (this.value == '') {this.value = 'Search ...';}" />
  4. </div>
  5. </form>

或者,您可以添加表单样式以匹配您的导航风格,例如:

  1. input.formInputText {
  2. margin-top7px;
  3. color#666;
  4. padding3px;
  5. background#ccc;
  6. }
  7. input.formInputText:hover {
  8. cursorhelp;
  9. color: 555;
  10. background#ccc;
  11. }

三、添加一个主页链接到您的导航菜单

  1. add_filter( 'wp_nav_menu_items', 'add_home_link', 10, 2 );
  2. function add_home_link($items$args) {
  3. if (is_front_page())
  4. $class = 'class="current_page_item"';
  5. else
  6. $class = '';
  7. $homeMenuItem =
  8. '<li ' . $class . '>' .
  9. $args->before .
  10. '<a href="' . home_url( '/' ) . '" title="Home">' .
  11. $args->link_before . 'Home' . $args->link_after .
  12. '</a>' .
  13. $args->after .
  14. '</li>';
  15. $items = $homeMenuItem . $items;
  16. return $items;
  17. }

只在特定的位置添加上述新增项目

新增项目默认将显示在所有自定义菜单中,这可能不是你所想要的,因此需要添加一个条件代码,让上述代码只执行在一个特定的菜单位置。

  1. function add_login_logout_link($items$args) {
  2. if($args->theme_location == 'Primary') {
  3. ob_start();
  4. wp_loginout('index.php');
  5. $loginoutlink = ob_get_contents();
  6. ob_end_clean();
  7. $items .= '<li>'. $loginoutlink .'</li>';
  8. }
  9. return $items;
  10. }

翻译水平有限,有不准确之处,请见谅,上述代码经测试全部有效。

posted @ 2013-01-16 20:55  meetrice  阅读(694)  评论(0编辑  收藏  举报