Just a quick code snippet for creating Collapsible menus for the WordPress Page and Category Sidebar widgets. It uses jQuery, Google Ajax libraries and in its current state works only when you set your WordPress permalinks to /%category%/%postname%/.
Warning: By no means is this the best way to make your WordPress Page and Category Sidebar widgets expandable but it worked for me so I figure i’d share it.
google.load("jquery", "1"); google.setOnLoadCallback(function() { jQuery("li.page_item a,li.cat-item a").each(function(){ if(jQuery(this).next('ul').length){ jQuery(this).after(' <a class="toggle_list" href="#">+</a>'); } }); jQuery("li.page_item > ul,li.cat-item > ul").hide(); jQuery("a.toggle_list").click(function(){ if(jQuery(this).next('ul:visible').length){ jQuery(this).html('+').next('ul').hide('fast'); }else{ jQuery(this).html('–').next('ul').show('fast'); } }); var h = window.location.href; jQuery('li.page_item a[href='+h+']').nextAll('ul').show(); h=h.substr(0,h.lastIndexOf('/')); jQuery('li.page_item a[href='+h+']').nextAll('ul').show(); jQuery('li.page_item a[href='+h+']').next('a').html('–'); }
This will add a plus sign to the end of any parent pages and collapse them once the jQuery library is downloaded; Your basic open and closing happens when you click the plus sign. It should also open up the menu section if you are in the section or a child section. Also this hasn’t been testing on a parent > child > child scheme.
RSS Feed
Twitter
Posted in
Tags: 
