2013-04-20 106 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