A drop down menu is often your best means of keeping the navigation simple and uncluttered, that can offer your theme and users 2 new things. One, add a nice new type of effect to enhance your theme. Two, allow the users to find things more easily. There are a few plugins that you can use to make this, but in this post, I’ll show you how to make your own WordPress Drop-Down menu with CSS and jQuery and WordPress’s standard wp_list_pages, wp_nav_menu, wp_list_categories
The aim of this short tutorial is help you build a simple multi-level drop-down menu in your WordPress theme using jQuery’s JavaScript library to make sure cross-browser compatibility.
To get started with this tutorial, you’ll need to have a WordPress website up and running and a willingness to alter your theme files. Once that’s squared away you’re ready to rock!
The Standard WordPress navigation layout
Before we get into making drop downs, we need to take a look at the way WordPress layouts it’s navigation.
[gist id=2886132]
Both wp_list_pages
and wp_nav_menu
lay out their menus pretty much exactly the same, besides different class names, so we’ll just take a look at wp_nav_menu
‘s printed code:
[gist id=2886158]
As you can see, WordPress neatly (well neat besides all the classes) layouts out the menu in a nice semantic list, with the children of the navigation nested inside of it’s parent li. This allows us to use both CSS and/or jQuery to make our drop downs work!
Styling the Navigation
Now, let’s styling the navigator. We want our list items aligned horizontally, and of course centered in the middle of the page.
[gist id=2886166]
jQuery Animation the Navigation
There’s also an easy way to make WordPress menus with jQuery drop downs. jQuery allows you to get a bit more fancy, by adding speeds and fades to the animation. It’s also useful if you’re still stuck supporting IE6.
There’s a tiny piece of jQuery required to show/hide the drop down menu’s. From an accessibility point of view, you can link the top-level navigation item so that if JS disabled, the user will still be able to get around your site. Here’s the script.
[gist id=2886171]
Finally!! A Drop Down WordPress Navigation Menu using CSS and jQuery!
That’s basically it, the example is about as stripped down as I could make it. Hopefully you’ll have fun bending the code to your next project. Feel free to post comments if you have any questions about the tutorial. Happy coding!
Hi Mr. Quan. This article is so useful. I like it so much. Thank you.
Hope you can use as your own design :)
Great post. You seem to have a good understanding that how to create a professional drop down menu. When I entering your blog, I felt this. Come on and keep writing your blog will be more attractive. To Your Success!
hi,
please help me.
how rename or add (dropdown menu) to style file.
Difference : “.sf-menu AND #cat-nav- ”
default file:
new code (DROPDOWN MENU):
Thanks
This is a nice post. Really helpful
I also got similar info. in http://www.7tech.co.in/wordpress/how-to-add-a-wordpress-drop-down-menu-to-your-wordpress-theme/ which is also good.
Thanks for sharing ,i would really love to try this at the earliest on my new theme :).
Does the jQuery-code goes into functions.php?
No, you put it into js file then load it from header, it’s work as javascript code