Longing for a break


jQuery, again!
July 11, 2009, 7:53 am
Filed under: Computing, Web, internet | Tags:

Working away at the day job earlier this week I had to find a solution for hiding menu items within a WordPress site where they didn’t belong to the current section.  I spent forbloodyever looking for a solution on day one.  I tried using code available on the WordPress codex site, no joy.  I tried searching the interwebs and using that code, again no joy.  Then I looked into just using pop-up menus, decided against it in the end.  Then finally as I was getting really frustrated I saw something on the codex site that said DER we apply classes to the currently selected menu item.  BING!  I then turned to my favourite little javascript library, none other than jQuery and applied the following code:

$(“#nav li”).not(“.current_page_item”).children().addClass(“hidden”);

This will hide any menu item not being the current page by adding the class “hidden” (aka display: none). That works well, but once you are in the sub-pages the menu hides as the parent(s) aren’t being display, of course I didn’t think of that and launched. Hmm. Another line of code was necessary:

$(“#nav li.current_page_item”).parents().removeClass(“hidden”);

This line will remove the class “hidden” from the currently selected pages parents, thus the current menu item will always display.

This unfortunately does have a slight visual annoyance in that you will see briefly those menu items to be hidden and re-shown, but it works.

I heart jQuery!


No Comments Yet so far
Leave a comment



Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>