I’ve been trying to get the function wp_nav_menu() to output the following html structure:
<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’. But the first one outputs the element before </li>
tag and the next one before </a>
tag. Is there any way to output the <span>
element after </li>
tags?
I read about the Walker class but I don’t quite understand its logic – I’m not that good at PHP.
I have no wp installed to test it, but still leave it here.
You need to extend the
Walker_Nav_Menu
class, like this:And then pass
new custom_walker_nav_menu
as$walker
parameter forwp_nav_menu()
function.Actually, this answer is uncomplete, can’t get how to get rid off last
<span>
element you don’t need. 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 ofafter
orlink_after
.