Displaying WordPress categories in a horizontal dropdown menu.

One of my readers recently asked how I created my horizontal menu bar: the short answer is by mixing CSS and Javascript.

The first step is to get WordPress to display the menu as a hierarchical list without a title. <?php wp_list_categories('sort_column=name&sort_order=asc&style=list&children=true&hierarchical=true&title_li=0'); ?>

We then wrap this WordPress code in the following so we can style it.

<div style="text-align:center;">
    <ul id="menu" style="padding:0; margin:0;">
        <?php wp_list_categories('sort_column=name&sort_order=asc&style=list&children=true&hierarchical=true&title_li=0'); ?>
    </ul>
</div>

I added this to my header.php, but you can add it anywhere you want it to appear.

The CSS is fairly simple and you just need to add it to your theme’s style.css file.

ul#menu {
	margin: 0;
	padding: 0;
	list-style: none;
	width: 100%;
	font-size:1.2em;
}

ul#menu li {
	float: left;
	padding: 0;
	margin: 0;
	border-right:solid 1px #fff;
}

ul#menu ul li {
	float: none;
	position: relative;
	border-bottom: 1px solid #7EAED7; /* fixes gap problem in IE */
	border-left: 1px solid #FFF;
	z-index:1000;
}

ul#menu li ul {
	margin: 0;
	padding: 0;
	display:none;
	list-style: none;
	position: absolute;
	background: #9CC;
}
ul#menu ul ul{
	margin-left: .2em;
	position: absolute;
	top: 0; /* if using borders, -1px to align top borders */
	left: 100%;
}

ul#menu * a:hover, ul#menu li a:active{
background:#7EAED7 !important;
color: #FFFFFF;
}

ul#menu li a:link,
ul#menu li a:visited,
ul#menu li a:hover,
ul#menu  li a:active{
	display: block;
	padding: .2em .3em;
	text-decoration: none;
	background: #5587B3;
	 color: #FFFFFF;
}


ul#menu ul li a:link,
ul#menu ul li a:visited,
ul#menu ul li a:hover,
ul#menu ul li a:active {
	width: 8em;
}

Of course you will need to change the colors and text sizes to ensure it blends with the rest of the theme.

Now the last step is the to make it work as a drop-down list on all browsers. This could be done in Firefox with a simple CSS declaration, but Internet Explorer doesn’t understand the :hover pseudo classes, so we mimic this in Javascript.

<script type="text/javascript">
 /*<![CDATA[*/

var mbA,mbT,mbTf,mbSf;
var mbR = [];

function mbSet(m) {
if (document.getElementById&&document.createElement) {
	var m=document.getElementById(m);
	mbR[mbR.length] = m;
	var i;

	e=m.getElementsByTagName('a');
	if (!mbTf) mbTf=new Function('mbHT();');
	if (!mbSf) mbSf=new Function('mbS(this);');
	for (i=0;i<e.length;i++) {
		e[i].onmouseout=e[i].onblur=mbTf;
		e[i].onmouseover=e[i].onfocus=mbSf;
	}

	m=m.getElementsByTagName('ul');
	for (i=0;i<m.length;i++) {
		mbH(mbL(m[i]));
	}
}}

function mbHA() {
	if (mbA) {
		while (mbA) mbH(mbA);
		mbHE('block');
	}
}

function mbHT() {
	if (!mbT) mbT=setTimeout('mbHA();', 0);
}

function mbTC() {
	if (mbT) {
		clearTimeout(mbT);
		mbT=null;
	}
}

function mbS(m) {
	mbTC();
	if (mbA) while (mbA&&m!=mbA&&mbP(m)!=mbA) mbH(mbA);
	else mbHE('none');

	if (mbM(m)) {
		mbSH(m,'block');
		mbA=m;
	}
}

function mbH(m) {
	if (m==mbA) mbA=mbP(m);
	mbSH(m,'none');
	mbT=null;
}

function mbL(m) {
	while (m && m.tagName != 'A') m = m.previousSibling;
	return m;
}

function mbM(l) {
	while (l && l.tagName != 'UL') l = l.nextSibling;
	return l;
}

function mbP(m) {
	var p = m.parentNode.parentNode;
	if (p.tagName == 'UL') {
		var i = 0;
		while (i < mbR.length) {
			if (mbR[i] == p) return null;
			i++;
		}
	} else {
		return null;
	}
	return mbL(p);
}

function mbSH(m,v) {
	m.className=v;
	mbM(m).style.display=v;
}

function mbHE(v) {
	mbHEV(v,document.getElementsByTagName('select'));
}

function mbHEV(v,e) {
	for (var i=0;i<e.length;i++) e[i].style.display=v;
}
/*]]>*/
</script>

A couple notes on the previous code.

  1. To activate it, change your theme’s <body> tag to <body onload=”mbSet(‘menu’);>
  2. It was not written by me, but I’ve been using it for a long time and don’t remember where I got it.
  3. It can occasionally create a JavaScript error, and I’ve intended to rewrite it for a long time, but it usually works fine.
Categorized as: WordPress

336 thoughts on “Displaying WordPress categories in a horizontal dropdown menu.

  1. Pingback: Display Categories In Horizontal Drop-Down Menu | Wordpress New Tricks For Begginers

  2. Pingback: WordPress 开发者资源 – 我爱分享

  3. Pingback: 40 Wordpress Most Wanted Tips by ehackworld | Ehackworld

  4. Pingback: 50个最强WordPress教程 | 85后IT宅男

  5. Pingback: 30个优秀WORDPRESS技巧 | 85后IT宅男

  6. Pingback: 150 Most Wanted WordPress Tips, Tricks, and Hacks for every Wordpress Blogger - TechnoGrate - TechnoGrate

  7. Pingback: 50 مرجع آموزش Ùˆ استفاده از وردپرس « بنیت

  8. Pingback: WordPress Developers Toolbox « Tybee Guy

  9. Pingback: WordPress Hacks to Style Out Your Blog Theme « CSS Tips

  10. Pingback: WordPress ????? | ???

  11. Pingback: 40+ Most Wanted Wordpress Tricks and Hacks | Wordpress Automatic

  12. Pingback: 20+ Most Wanted WordPress Tricks and Hacks | eolthing.com

  13. Pingback: 40+ Most Wanted WordPress Tricks And Hacks « Dragonfly Social Connection

  14. Pingback: 70+ WordPress Tricks and Hacks « nikilakiran

  15. Pingback: Top 50 WordPress Tutorials « Web Development informations

  16. Pingback: 40+ Most Wanted WordPress Tricks And Hacks | Technoric Hub - Huge collection of information

  17. Pingback: 50???wordpress?????????-????

  18. Pingback: 50???WordPress?? | *^_^*???-zhm

  19. Pingback: WordPress ????? | ?? Naoxx ??

  20. Pingback: Top 50 WordPress Tutorials | Black Ant Graphic

  21. Pingback: 135+ Ultimate Round-Up of Wordpress Tutorials | Tutorials | instantShift - Web Design Magazine

  22. Pingback: WordPress Tips, Tricks, and Hacks | Launch Your Site

  23. Pingback: WP?????-2.3: WordPress??????? | ????

  24. Pingback: ??????????? ?? ???? ????? ???? ?????????? ! »

  25. Pingback: 150 Most Wanted WordPress Tips, Tricks, and Hacks | Ahmed Mohsin

  26. ok i changed themes and figured out how to add the menu, but i can’t get it to be a dropdown menu. Also, how can i add both pages and categories to the same menu?> I apologize in advance if I sound lost with all of this….because i am :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>