I’m looking for a solution to adjust the output of wp_nav_menu
only on a specific navigation menu. I have my menu:
wp_nav_menu(
array(
"container" => "nav",
"container_class" => "container",
"container_id" => "nav",
"fallback_cb" => false,
"menu_class" => "six columns omega main-nav sf-menu",
"theme_location" => "main-nav"
)
);
The Problem
I need to find a way insert <span>[number]</span>
inside of the anchor element. [number]
represents the number of the item starting at 1
. Here’s a visual:
The structure of the the menu would be:
<li class="...">
<a href="#"><span>01.</span>Home</a>
</li>
<li class="...">
<a href="#"><span>02.</span>Services</a>
</li>
<li class="...">
<a href="#"><span>03.</span>Portfolio</a>
</li>
Currently, I am using a jQuery hack. This works but there’s no guarantee that the user can merely disable JavaScript. I’ve looked at:
- wp_nave-menu(): Add span tags inside a tag that is parent — StackOverflow
- Understanding the Walker Class – Hub.TutsPlus.com
I have an idea that extending the walker class is necessary but my ultimate confusion lies with building the links manually and inserting dynamic information before link text is printed. The Stack Overflow question does it but does not explain how to get there.
Addendum
To get the precise result that I wanted see pastebin. $item->title
needed to be modified in order to follow the HTML structure as listed above.
You just need to create your own walker class and extend
start_el
method. This method buildsa
link and you will be able to add yourspan
before it:After it you will be able to use this class in your
wp_nav_menu
function call: