Accordion Menu for WordPress

The navigation menu in our theme Lavender is in the sidebar, and in order to keep it above the fold, at least initially, we want to collapse the nested levels using an accordion effect.


  1. Hierarchical menu
  2. Only leaves are links
  3. Link to pages, categories, or anything?
  4. jQuery (WP’s?)

Searching for a WP plugin…

  1. Super Slider: MooTools based.
  2. BySlideMenu:  MooTools, horizontal, images.
  3. Accordion Image Menu: MooTools, images, with “lightbox” effect.
  4. Tabbed Widgets: jQuery UI. Messed with it for quite a while — unintuitive. Makes “tabs” of other widgets in the widget area, but…

… Nevermind

Can’t find a simple, usable plugin.I mean, there’s Accordion Menu, which only works with the Pages widget (hardcoded to li.widget_pages), limited to three levels, and the code looks convoluted. Foo?

But this ought to be trivial to hack with jQuery:

  1. Add a listener on clicks to list items;
  2. Collapse everything initially (ie, on document ready);
  3. Toggle visibility when clicked;
  4. Use WP’s jQuery; make sure it’s inserted when the menu is rendered.


The DOM we expect:

            a "Foo"
                  a "Bar"

Algorithm for the event handler is now apparent: if the clicked anchor is followed by an unordered list sibling, toggle that UL’s visibility:

function() {
	var sub = jQuery(this).next("ul");
	if (!sub.length)
		return true; //??? Is it necessary?
	if (":hidden"))
	return false;

Now apply this handler to anchors in the menu:

jQuery('ul#menu-navigation > > a').click(…);

and run all this on document ready.

Oh, and collapse them initially:

jQuery('ul#menu-navigation > > ul.sub-menu').hide();

Comments are closed.