Folding menu plugin for WordPress

A while ago I wrote a post about creating a folding menu for WordPress built in Pages widget using only jQuery without adding or modifying any markup or PHP. A reader, Kretschmar, reported some bugs and asked if it could work for more than one level.

After modifying the jQuery for it to work on multi level depth I created a simple plugin for WordPress. See the demo of ILC Folding, go and click on Page 2.

Here’s the jQuery code:

jQuery(document).ready(function(){
jQuery(".page_item ul").hide();
jQuery(".current_page_item ul:first").slideDown();
jQuery(".current_page_item").parents("ul, li")
.map(function () {
jQuery(this).slideDown();
});
});

So, after sliding down the first parent and getting all parents of current_page_item, we  slide down each parent level by level until the list is empty.

I packed everything and created a WordPress plugin so you can download the folding plugin for WordPress and start collapsing those Pages.


“Folding menu plugin for WordPress” received 57 comments! Add yours.

  1. Tadd March 14th, 2009

    This looks fabulous! I’ll have to check it out – thanks for your hard work!

  2. WordPress Plugin Releases for 03/14 | TechnoBlog March 15th, 2009

    [...] Folding Menu Plugin [...]

  3. Elliot March 15th, 2009

    Tadd: I’m glad it saw the light. It could’ve been sleeping the eternal dream forever. Hopefully now it will be useful to someone.

  4. darkar March 15th, 2009

    Say it!

  5. Andrea March 18th, 2009

    =D
    see the light
    it will be useful for me too probably
    thnx
    i’m just learning php and such things about wordpress jquery blabla i have no idea you know xD

  6. Andrea March 18th, 2009

    i forgot it
    very nice pageeee!!!!!
    is really cool i love it ^_^

  7. Elliot March 18th, 2009

    Thanks Andrea for your kinds words.

  8. Dave March 19th, 2009

    It would seem like a ‘demo’ / example would be essential to see what this does?!

    Thx

  9. al March 22nd, 2009

    May I ask where should one paster the wp_enqueue_script? Into a special file or into the head section? And the should js file be placed?

  10. Elliot March 22nd, 2009

    al, first of all, the download provided here is a WordPress plugin. Install it and activate it like any other WP plugin and that’s it.
    The wp_enqueue_script sentence must be placed inside a hook linked to the `init´ action.

  11. Mike Salway March 26th, 2009

    I’d love to see an example/demo too please.

    btw great website, beautiful design.

  12. Elliot March 26th, 2009

    I’ve set up a vanilla wordpress to demo this plugin. Go to ILC Labs and click on Page 2.

  13. Delicious SEO Bookmarks » SEO Update March 27th, 2009

    [...] :: Folding menu plugin for WordPress :: – WordPress Plugin folding menu [...]

  14. Steve March 27th, 2009

    Hi, this looks like what im after, however im not sure how to implement it. I have installed the plugin, and have the normal pages widget in use. What do i need to do to get this to work, at the moment all i get is the normal parent pages displayed by the pages widget. Im sure i have missed the point somehow, im compitant in XHTML and CSS, but only started using wp LAST WEEK!
    thanks

  15. Elliot March 27th, 2009

    Steve, install/activate the plugin and add some parent pages. Add then some children to parent pages. Visit your site and click on a page that has children pages. When the page is loaded the children will be revealed.

  16. Elliot March 27th, 2009

    I’ve set up a vanilla wordpress to demo this plugin. Go to ILC Labs and click on Page 2.

  17. Steve March 30th, 2009

    great, sorted.
    Only other thing, the child pages on my site are not indented, is this something i would need to sort in my css file?

  18. Elliot March 30th, 2009

    Yes, you should apply your own styles. You can check the WordPress default theme to see which classes are used.

  19. Peter March 31st, 2009

    I’m working on a site and just used this plugin.

    It works great, but the only thing i dislike, is that it keeps refolding after clicking on one of the ‘children’.

    Is there a way to just keep it fold out, when it is already?

    Thanks in advance!

  20. Stuart April 1st, 2009

    Testing this plugin, and I like it a lot. But it seems to auto-expand different child pages at will – is there a way to force it to collapse all but the current tree?
    Thanks!!!

  21. Elliot April 2nd, 2009

    Peter, Stuart: maybe I can add a switch for those who want it to operate that way.

  22. Stuart April 2nd, 2009

    That would be great. Apart from that, 10 out of 10!

  23. Falco Stellare April 2nd, 2009

    Great plugin!!! It would be nice to have a feature to slide down the sublevels without entering the first-level page…

  24. Peter J Harrison April 7th, 2009

    Great plugin, wish wp had this functionality by default! Great work!

  25. Elliot April 7th, 2009

    Peter, I think that the beautiness of WP lies in the fact that it is quite small, so is not bloated with a lot of code. I’ve always loved the C programming language due to this. It’s small but you can build a skyscraper on top.

  26. Peter J Harrison April 7th, 2009

    Very well said!

  27. Michael April 10th, 2009

    Thank u for the nice plugin.

    I am testing the plugin and works fine with pages but when you a add a category the plugin does not remain open on the current li, is there a workaround ?

  28. Elliot April 10th, 2009

    This plugin is intended to be used with the Pages widget. I could add later some code to catch the categories folding, but I’m currently quite busy with work so it will have to be after one week.

  29. Edward June 18th, 2009

    Worked perfect buddy. Thanks for developing this.

  30. Elliot June 20th, 2009

    No prob. I’m glad it’s useful to you.

  31. Wordpress Navigation & jQuery - IOException.de June 22nd, 2009

    [...] aktuellen Seite anzuzeigen und den Rest einzuklappen. Bei der Suche nach einer Methode bin ich auf Folding menu for WordPress gestossen. Allerdings fand ich die Animation bei jedem Klick etwas [...]

  32. parent talk August 16th, 2009

    a new parenting blog for parent discussions chats and natters http://parentinggossip.blogspot.com/ get it all out

  33. Vroni August 23rd, 2009

    Thank you so much for this. I’ve searched so long for something like that. Great …

  34. Edward August 25th, 2009

    Thanks for the great work you do. Appreciate your valuable time.

  35. Digital Nomad September 13th, 2009

    Thanks for sharing the plugin. Will try it out today!

  36. Useful resources « I heart cclab October 16th, 2009

    [...] Posted in Resources jquery menu: http://www.ilovecolors.com.ar/folding-menu-plugin-wordpress/ [...]

  37. Jens Törnell October 20th, 2009

    This works great! Simple way to solve it!

  38. David November 16th, 2009

    Amazingly simple.. If your not paying attention you might not even know that it’s working. And it exactly what I have been looking for.

    Thanks!

  39. Carl January 20th, 2010

    Your are amazing and I love you.

  40. Angela January 21st, 2010

    This is EXACTLY what I have been looking for. Thank you so much for working this into a plugin. Your work is very much appreciated!

  41. Elio January 24th, 2010

    Thanks Angela, I really appreciate your kind words.

  42. phill Taare February 11th, 2010

    firstly thanks for your plugin it’s really
    brilliant, I’m trying to modify it however to act like an expanding/collapsing menu and less like an accordian eg:http://www.i-marco.nl/weblog/jquery-accordion-menu/index_collapsed.html#
    However my programming is basic at best, anyways thanks I appreciate your plugin.

  43. Elio February 11th, 2010

    Hey Phill, it’s looking good!

  44. Nico July 27th, 2010

    Hi,
    great plugin!! i have a question: i have more menù, but folding works only on first, there is the possibility to applu folding on every menù??

  45. Elio July 28th, 2010

    Nico, I will update the plugin later, but for now just replace the javascript code of folding.js with the following:
    jQuery(document).ready(function(){

    jQuery(“.page_item ul, .page_item .children, .menu-item ul”).hide();

    jQuery(“.current_page_item ul:first, .current_page_item .children:first, .current-menu-item ul:first”).show();

    jQuery(“.current_page_item, .current-menu-item”).parents(“ul, li”)
    .map(function () {
    jQuery(this).show();
    });
    });

  46. Jason July 30th, 2010

    NOt working in IE???! :(

    all menus appear “expanded”! :(

  47. Popper August 17th, 2010

    The demo-link is broken. Please let us see a demo..

  48. myish August 25th, 2010

    ey mate,

    hell knows how many times i tried but im such a noob in php and the works. i’m using sandbox and i couldn’t make it work. please help. thanks!

  49. Anne September 30th, 2010

    I can’t add anything to my sidebar without it making the menu disappear all of a sudden. Is there a solution to this new problem?

  50. Elio September 30th, 2010

    It’s not a problem. It’s the way WordPress works. If you have an static bar that supports widgets, whenever you add a widget the other static content will dissapear to make room to widgets. Just keep adding the widgets you need.

  51. Anne October 1st, 2010

    “Just keep adding the widgets” and …?
    Your menu will reappear after a while?
    Sorry I don’t quite understand.
    Maybe I should add widgets to a second sidebar instead.

  52. Elio October 1st, 2010

    Anne, if your site is greenlandwhitefront.org, it is currently not using Widgets. The menu is generated in what we call an static sidebar. If the sidebar is (and I assume it is) widgetized, you should first add the Page widget and then the Category widget. Here’s a doc from wordpress.org regarding the use of widgets.

  53. nico November 25th, 2010

    hey! …quite a while ago you talked about the possibility to make a “switch”….for those who don’t want to slideup the children when clicking on them… was this already solved? thanks.nico

    Elliot April 2nd, 2009

    Peter, Stuart: maybe I can add a switch for those who want it to operate that way.

  54. Harry January 21st, 2011

    It works fine in Firefox & Chrome, but it doesn’t work properly in IE:

    Here is the code i’m using:

    jQuery(document).ready(function(){
    jQuery(“.page_item ul”).hide();
    jQuery(“.current_page_item ul:first”).slideDown();
    jQuery(“.current_page_item”).parents(“ul, li”)
    .map(function () {
    jQuery(this).slideDown();
    });
    });

    For some reason IE appears to fail on this line:

    jQuery(“.current_page_item”).parents(“ul, li”)

    Is anyone able to advise how I can fix this so that IE recognises this code?

    Please help!! It’s a great plugin but I really need it to work in IE as well.

  55. m4niac February 7th, 2011

    wonderful, thank you!

  56. Donna June 15th, 2011

    Hello,

    I recently updated my theme and my WP installation, so I am not sure which caused this problem, but ILC Folding was working perfectly. Mainly, my side navigation used to display (top and sub-levels) and highlight appropriately (current page item), then it started doing this weird thing that when you clicked on a parent level, or a subnavigation menu item that was in a submenu already displayed, it reloaded the entire side menu again. To fix this I used the following code:

    #left ul li ul{ display: none; }
    #left li.current_page_parent ul { display: block !important;}

    This seemed to do the trick, and it does for the first and second levels of my side navigation, but when you drill down to the third level and below, all of the third level menus get displayed, even for items that are not currently active.

    This code fixes that:
    #left li.current_page_parent ul.children li.current_page_item { display: block !important;}

    But then you get the reloading of the entire menu again. If you look here you will see what I mean: http://arboretum.harvard.edu/library/image-collection/the-arnold-arboretum-captured-in-time-1982-1987-photographs-by-corliss-knapp-engle/

    I hope this makes sense, I would really appreciate any help, I am stuck.

    Thanks!

  57. Birgit July 8th, 2011

    Hi there, like Anne above I too have a problem with this plugin in IE7. When I click an ancestor page link, the menu folds out and the children pages will display briefly, but then disappear. The space they take up will be visible (there’s a gap between the current ancestor menu link and the next one), but there’s no clickable zone where the sub pages should’ve been displayed, just the empty space (tested this because I suspected the link colour was the culprit). Any help would be greatly appreciated!

Leave a comment