2013-04-20 39 views
0

我需要帮助!WordPress的:如何添加推荐变量wp_nav_menu链接

wp_nav_menu()创建HTML输出

<li id="xxx" class="xxx"><a href="http://localhost/?page_id=1">home</a></li> 
<li id="xxx" class="xxx"><a href="http://localhost/?page_id=2">news</a></li> 
<li id="xxx" class="xxx"><a href="http://localhost/?page_id=3">reviews</a></li> 

我该如何去转诊变量添加到链接?像这些:

<li id="xxx" class="xxx"><a href="http://localhost/?page_id=1&ref=abc">home</a></li> 
<li id="xxx" class="xxx"><a href="http://localhost/?page_id=2&ref=mno">news</a></li> 
<li id="xxx" class="xxx"><a href="http://localhost/?page_id=3&ref=xyz">reviews</a></li> 

任何帮助将不胜感激。

更新: 我得文字名称(家庭,新闻和评论)这样的:

<li id="xxx" class="xxx"><a href="http://localhost/?page_id=1&ref=home">home</a></li> 
<li id="xxx" class="xxx"><a href="http://localhost/?page_id=2&ref=news">news</a></li> 
<li id="xxx" class="xxx"><a href="http://localhost/?page_id=3&ref=reviews">reviews</a></li> 

它将被用作类别名称。多http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output

这应该使它:

回答

1

你可以过滤导航菜单输出在functions.php

function add_ref_value($items, $menu, $args) { 
    foreach ($items as $item) { 
     // check some value in the $item object and generate ref value 
    } 
    return $items; 
} 

add_filter('wp_get_nav_menu_items', 'add_ref_value', null, 3); 

更新:其实,我想你会更好使用这种方法更容易生成并追加你的ref属性。

这里有几个环节,以帮助您:

+0

这里就是在我function.php <?PHP的,如果(function_exists( 'register_nav_menus')){ \t register_nav_menus(阵列( 'topmenu'=> __('Top Menu'))); } ?> 我是新来的wordpress和php,但我会尽力弄清楚这一点。谢谢。 – s4m 2013-04-20 16:08:44

+0

没问题,第一个链接是特定于你的情况,但tutsplus链接有一个沃克类的一个很好的描述,如果事情变得混乱。 – Joe 2013-04-20 16:10:08

+0

你是使互联网如此棒的人之一。非常感谢你@Joe – s4m 2013-04-20 16:18:21

1

修复!我编辑NAV-菜单的template.php在线路86

$attributes .= ! empty($item->url)? ' href="'. esc_attr($item->url) .'&ref=' .$item->title. '"' : ''; 

我添加& REF =” $。用品 - >标题。 '

这可能会帮助其他人,特别是如果他们想从url获取变量。

我想学习学步车类,但是....再次感谢@joe