Just another idiot with a blog

jQuery Accordion for Wordpress

So I was working on a site with a vertical navigation today and the previous developer working on it made the entire navigation static….Well thats pretty much worthless for a CMS like Wordpress. So I took it upon myself to create an accordion script for Wordpress (it can be used wtihout Wordpress just as easily). I commented the code nicely and you can essentially paste this directly into wordpress and it will work.

The File Structure is as follows

<ul>
   <li><a href="http://link"></a>
        <ul>
            <li><a href="http://subnavlink">LINK</a></li>
        </ul>
   </li>
</ul>

The CSS You will need

#navigation ul li ul { display:none; }
#navigation .displayMe { display:block; }

Now for the Javascript. This one is for all your Wordpress Developers who need a custom jQuery Accordion

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">

$(document).ready(function() {	

//If its a page parent (based off wordpress), add the class "displayMe"
//This way the accordion will be opened up on the page you are on.
if ($('#navigation ul li').hasClass("current_page_parent")) {
	$('#navigation .current_page_parent ul').addClass("displayMe"); }

//Hide the submenus
$('#navigation ul li ul').hide();

//Add a class to the parent li IF it has sub UL's
$("#navigation ul li:has(ul)").addClass("theDon");

//Da henchman
$("#navigation ul li ul li:has(a)").addClass("henchmen");

//Remove the link if it has a submenu
$('#navigation .theDon > a').attr('href', '#');

//When you click it, toggle bitch.
$('#navigation ul li a').click(
function() {

//Onclick Remove the class dipslay me which is only display:block;
//This way they can close it if they click it or it will glitch
$(this).next().slideToggle('slow').removeClass("displayMe");

//return false so the # doenst move view to the top of the page
if ($(this).attr('href') == '#') { return false; }

//Close it all out
	});
 });

</script>

<div id="navigation">
<ul>
<?php wp_list_pages('title_li='); ?>
</ul>
</div>

Hope this jQuery Accordion with an emphasis for Wordpress helps!

Cheers,
-Wes

18 Responses to “jQuery Accordion for Wordpress”

  1. april says:

    nice one wes!

  2. Jess says:

    so will this work for categories instead of pages? for ex: your two categories jQuery and random. could i get the accordion to show/hide posts within a category?

  3. admin says:

    Yes, just use
    < ?php wp_list_categories('title_li='); ?>

    You can use the css that is added when it was subcategories, e.g. “theDon” and “henchmen” to style as needed. Let me know if you have any questions or just hit me up on chat (top of the sidebar)

  4. archil says:

    Two thumbs up… Am I the previous developer?… Lol…

    Just continue posting some of your tips dude… It really helps.
    I’m digging your post now…

  5. Jamie says:

    Love the accordion. I was curious, is there a quick way to alter it so that only one group of sub-pages stays open at a time?

  6. Slebee says:

    Nice work – just the code I was looking for.

  7. admin says:

    For all that wanted the modified accordion, check it out

    http://www.wessray.com/labs/accordion.php

    good stuff.

  8. payday loans says:

    The author of http://www.wessray.com has written an excellent article. You have made your point and there is not much to argue about. It is like the following universal truth that you can not argue with: You can not drink until you are sober. Thanks for the info.

  9. Thorsten says:

    Hej,
    just found your script and everything works fine. but it seems that the addClass(”displayMe”); } functionality has no effect on my wordpress-output. wordpress gave me the »current_page_item« but the script doesn’t affect that the parent will get the »displayMe«-effect. Any hints what i can do? thank you very very much, cheers

    Weingut

    Familie
    Philosophie
    Fakten
    Weinlagen

  10. giles says:

    brilliant~!
    I’ve been trying to hack my way to this for 8 hours!
    Cheers,
    Giles

  11. giles says:

    hi,
    one question – any idea how to insert a rel tag (ie. rel=”shadowbox”) into the valid link – the child page with content at the end of a parent wp_list_pages tree. You’ve managed to null “#” the parent accordion pages, i want something like that for the final link to the actual page. I’m hoping to use thickbox / shadowbox for my page content, so as to not reload the screen and lose the user’s place on the accordion.
    Any ideas?
    Thanks again

  12. admin says:

    giles, add $(’#navigation ul li ul li a’).attr(’rel’,'thickbox’); you can see it in the source here http://www.wessray.com/labs/accordion.php

  13. giles says:

    Thanks wes, that works pretty well (not fetching my page but will sort that out)
    Is there any way you can get a jQuery script to search through #navigation ul li etc. until it finds the last href link in the tree. I’m using wp_page_list and would like be able to nest the Page with the actual content (the final child) at any level of depth ie.

    accordion>accordion>accordion>link

    Or

    accordion>accordion>link

    etc. If you can think of any relevant jQuery that would be sweet.
    Thanks!

  14. Stan says:

    hi wes, many thanks for this code! i tried to write something like this myself, but it was no good. you really saved my butt!

  15. casino en ligne…

    La plupart des casinos en ligne ont une incroyable variété de machines à sous à choisir….

  16. The anti-aging industry has exploded in popularity and growth over the past ten years. More and more people are trying to fight the one disease that every….

    More and more people are trying to do something about their aging skin. Fine lines and wrinkles cause undue stress to men and women all over the world…

  17. The anti-aging industry has exploded in popularity and growth over the past ten years. More and more people are trying to fight the one disease that every….

    More and more people are trying to do something about their aging skin. Fine lines and wrinkles cause undue stress to men and women all over the world…

  18. Great Post!…

    [...] I found your entry interesting thus I’ve added a Trackback to it on my weblog :) [...]…

Leave a Reply

Copyright ©2010 wessray.com
Protected by Copyscape Unique Content Validation