0

我需要帮助!

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>

它将用作类别名称。

4

2 回答 2

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 );

更新:实际上,我认为你最好使用这种方法:http ://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output

这应该使生成和附加ref属性变得更加容易。

这里还有一些链接可以帮助您:

于 2013-04-20T13:21:50.553 回答
1

解决它!我在第 86 行编辑了 nav-menu-template.php。

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

我添加了 &ref=' .$item->title。'

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

不过我想学习步行者课程....再次感谢@joe

于 2013-04-21T05:58:09.057 回答