[英]Custom menu tags in wp_nav_menu
I've been trying to get the function wp_nav_menu() to output the following html structure: 我一直在尝试获取函数wp_nav_menu()来输出以下html结构:
<ul> <li><a href="#">LINK</a></li><span>element</span> <li><a href="#">LINK</a></li><span>element</span> <li><a href="#">LINK</a></li><!--no element HERE!--> </ul>
I tried with parameters like 'after' and 'link_after'. 我尝试使用'after'和'link_after'之类的参数。 But the first one outputs the element before
</li>
tag and the next one before </a>
tag. 但是第一个输出
</li>
标记之前的元素,第二个输出</a>
标记之前的元素。 Is there any way to output the <span>
element after </li>
tags? 有什么方法可以在
</li>
标记后输出<span>
元素? I read about the Walker class but I don't quite understand its logic - I'm not that good at PHP. 我了解了Walker类,但是我不太了解它的逻辑-我不太擅长PHP。
I have no wp installed to test it, but still leave it here. 我没有安装wp对其进行测试,但仍将其保留在此处。 You need to extend the
Walker_Nav_Menu
class, like this: 您需要扩展
Walker_Nav_Menu
类,如下所示:
class custom_walker_nav_menu extends Walker_Nav_Menu
{
public function end_el( &$output, $item, $depth = 0, $args = array())
{
$output .= "</li><span>element</span>\n";
}
}
And then pass new custom_walker_nav_menu
as $walker
parameter for wp_nav_menu()
function. 然后将
new custom_walker_nav_menu
传递为wp_nav_menu()
函数的$walker
参数。 Actually, this answer is uncomplete, can't get how to get rid off last <span>
element you don't need. 实际上,这个答案是不完整的,无法摆脱您不需要的最后一个
<span>
元素。 I'll try to improve it later. 稍后我会尝试改善它。 Also as @ rnevius mentioned in his comment it would be an invalid HTML, so you should definately use one of
after
or link_after
. 就像@ rnevius在他的评论中提到的那样,这将是无效的HTML,因此,您必须使用
after
或link_after
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.