We are using a jQuery UI accordian to render a menu in the sidebar. We’ve already written the markup and javascript and I am just trying to get the wp_nav_menu to output the same markup we’ve already written. I am creating a custom Walker Class extending Walker_Nav_Menu.
Here is the markup we are going for
<h2><a href="#">Parent Item</a></h2>
<div>
<ul>
<li><a href="">Child Item 1</a></li>
<li><a href="">Child Item 2</a></li>
<li><a href="">Child Item 3</a></li>
<li><a href="">Child Item 4</a></li>
<li><a href="">Child Item 5</a></li>
</ul>
</div>
<h2><a href="#">Parent Item 2</a></h2>
<div>
<ul>
<li><a href="">Child Item 1</a></li>
<li><a href="">Child Item 2</a></li>
<li><a href="">Child Item 3</a></li>
<li><a href="">Child Item 4</a></li>
<li><a href="">Child Item 5</a></li>
</ul>
</div>
Unfortunately I cannot get the custom Walker Class to output the markup how I’d like. Any examples or tutorials on the Walker Class would be great. Still haven’t found exactly what I’m looking for on SE or Google yet.
Thanks
The easiest way is to extend the
Walker_Nav_Menu
class rather than theWalker_Class
, (as the parent / ID fields are set and often you want to maintain some of the mark-up etc).The main methods are:
start_el
/end_el
– responsible for displaying an element in a liststart_lvl
/end_lvl
– responsible for displaying a sub menuAlso, there are
walk
anddisplay_element
which largely just control the mechanics of the class. For most purposes the above four functions are those that are needed to be altered in an extending class.That said, the structure you are after isn’t really nested, so you aren’t really using the Walker Class to its full extent here. However this custom walker class will get you most of the way there:
This is just a very simply class to demonstrate what you need to do. Since your structure isn’t nested – this might not look right if we go down further than 2 levels (i.e. to grand-children).
Usage:
(I’ve recently written this tutorial on the Walker Class: http://wp.tutsplus.com/tutorials/creative-coding/understanding-the-walker-class/ that you might find helpful)
Wish I’d seen your answer before I went down the rabbit hole. However I’ve been able to replicate the markup provided by our designer using an extension of the Walker class and it’s working great with the jQuery UI Accordion.
That got me pretty far. But I still had an issue with wp_nav_menu wrapping the entire thing in a UL. This broke the jQuery Accordion script. So I needed to remove it and after experimentation ended up rendering the menu with the custom Walker as follows…
Note: I tell the menu not to echo itself in the arguments. Instead I run a regular expression on it to remove that parent level UL.
Also of note.. This will look like crap if you don’t tell your jQuery Accordion NOT to autoheight.
The result is somewhat close to the original markup from my designer, aside from the classes WP adds in.