Subscribe to our feed...

http://feeds.feedburner.com/design-notes

  • Push Your Designs To Perfection

    goldSCHNITT is a small stand alone application for MAC that helps you apply golden ration to your design. Golden ratio(1.61803) is believed as a ratio of perfection. It has been used by many artists and architects through the history.

    Now we have this small app called goldSCHNITT to help you implement golden ration into your design.

Make A Dynamic Page Accordion Menu With jQuery

Under: Wordpress, jQuery 26 comments

There are many ways to add an flexible accordion menu to your web page. Most of them use div or paragraph but in this post I’m going to use unordered list and jQuery toggle to make an accordion menu.The most important information here is how to toggle list and how to hide/show the related page to viewer. I explained here specifically on dynamic page display with WordPress, you can always take the same theory and apply in other situations.

    Features:

  1. Work in Wordpress
  2. Accordion Slide
  3. Show only top level page list when visiting top level pages
  4. Show current page’s child page
  5. Show allpages when visiting child page

Wordpress Code to display page list

I got this code from wordpress.org

<?php
  if($post->post_parent)
  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
  else
  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
  if ($children) { ?>
  <ul>
  <?php echo $children; ?>
  </ul>
  <?php } ?>

The code displays all pages. This is the html code generated by Wordpress

<div id="page-list"><ul>
<li class="page_item page-item-1611"><a href="http://design-notes.info/archives/" title="Archives">Archives</a></li>
<li class="page_item page-item-742"><a href="http://design-notes.info/blogroll/" title="Blogroll">Blogroll</a>
<ul>
<li class="page_item page-item-1683"><a href="http://design-notes.info/blogroll/this-is-a-demo-page/" title="This is a demo page">This is a demo page</a></li>
 
</ul>
</li>
<li class="page_item page-item-1096"><a href="http://design-notes.info/linking/" title="linking">linking</a></li>
<li class="page_item page-item-113"><a href="http://design-notes.info/about/" title="About">About</a></li>
</ul>
</div>

Javascripts

Now what we need to do is to create accordion with jQuery’s toggle function and make it dynamically display the page you want your reader to see. Wordpress’s “current_page_item” class is very useful here. I used it on line 5 to list parent pages and again on line 9 to show its child pages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$(document).ready(function()
{//hide child page
$('#page-list ul li.page_item ul').hide();
//when navigate to a child page show all pages
$('#page-list ul li.current_page_item').parent("ul").show();
//show page list when toggle
$('#page-list ul li.active ul').show();
//show cerrent page's child page
$('#page-list ul li.current_page_item ul').show();
 
	 $('#page-list ul li').click(function() { 
$(this).addClass("active");
                    $(this).children('ul').slideToggle("slow"); 
            });
 
 
});

CSS

#page-list {
float:left;
clear:left;
margin:20px 0 0 10px;
display:block;
position:relative;
}
#page-list ul {
	list-style:none;
	margin:0;
	}
#page-list ul li{
padding:0 10px 0 10px;
margin:2px 0;
display:block;
cursor:pointer;
background-color:#ccc;
}
#page-list ul li ul li{
padding:0;
margin:0;
position:relative;
 
}
#page-list ul li ul li a{
	}
#page-list ul li a{
padding:3px 5px;
display:block;
width:150px;
background-color:#f4f4f4;
}

Conclusion

I first displayed all pages then hide or show them dynamically with jQuery. Its quite handy than using php alone. You can check out the demo page list on the sidebar by navigating to pages. I made a couple of demo pages for you too.

ADD TOAdd To Evernote

If this helps, why not tell your friends?

  • Reddit
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Technorati
  • Design Float
  • Identi.ca
  • FriendFeed
  • Yahoo! Buzz
  • Ping.fm

26 Responses to “ Make A Dynamic Page Accordion Menu With jQuery ”

  • ????? says:

    ???????. ???? ????? ????????? ?? ??? ?? ???? :)..

    [Reply]

  • alexandra says:

    you can check another site used this script
    http://redstores.com/?page_id=117

    [Reply]

  • Astrid says:

    Ahhhh!!! Please could you share where goes what to???
    Like the Javascrpit? where it goest to, do i copy and create a own one ant then link it somehow to all pages??? Love the soulution but hard to follow hot to make it…

    Much Love Astrid
    Astrid´s last blog ..16 months old Jaxie

    [Reply]

    alexandra Reply:

    javascript goes above head tag. I did not provide download cause all the answers are here, the process of learning is about trying to get it work. Its not only copy past.

    [Reply]

  • Lems says:

    This doesn’t work in IE :(

    [Reply]

    alexandra Reply:

    It has been tested in IE8 with no problem

    [Reply]

  • hmmmm says:

    cant find a thing here…where are the links to demo pages?
    hmmmm´s last blog ..Family Picture

    [Reply]

    alexandra Reply:

    you can navigate to my pages and look at the demo on the right side or visit this site
    http://redstores.com/?page_id=117

    [Reply]

  • Danny says:

    Nice, is there a way to use it outside of Wordpress?

    [Reply]

    alexandra Reply:

    Well I used wordpress default classes. In order to use with other CMS you have to read their code and figure out.

    [Reply]

  • scott shanks says:

    Hi thanks for posting this tutorial, is there a working example online anymore? I don’t see anything in your pages or the site you posted that has an accordion menu.

    Thanks!

    Scott

    [Reply]

  • I like it very! I used it in same projects. It also works in Wordpress!

    [Reply]

  • media. says:

    Nice but where can I see the demo?

    [Reply]

    alexandra Reply:

    Sorry the demo page is down I ll think of some other ways to show it in the future

    [Reply]

  • Jussi says:

    Hi,

    Great work. However, is it possible to make menu to be open by default. Now it’s always closed until of course I click to open.

    Also, how can I make post title to open menu, so that by default it actually shows only title that I have specified and when I click the title, it shows menu?

    Title
    •menu item1
    •menu item2

    I’m not so good with javascript :) Also, that demo would be really nice.

    [Reply]

    alexandra Reply:

    Sorry the demo was linked to one of my projects but the client decided to change design so I dont have a working demo anymore. However, Im building another theme now I ll implement this technique and get it online asap

    [Reply]

  • Leave a Reply

    All fields marked with "*" are required.