Oh, as an Aside: Displaying WordPress categories in a horizontal dropdown menu. (From the 16th of January)

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.

  1. <div style="text-align:center;">
  2. <ul id="menu" style="padding:0; margin:0;">
  3. <?php wp_list_categories('sort_column=name&sort_order=asc&style=list&children=true&hierarchical=true&title_li=0'); ?>
  4. </ul>
  5. </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.

  1. ul#menu {
  2. margin: 0;
  3. padding: 0;
  4. list-style: none;
  5. width: 100%;
  6. font-size:1.2em;
  7. }
  8. ul#menu li {
  9. float: left;
  10. padding: 0;
  11. margin: 0;
  12. border-right:solid 1px #fff;
  13. }
  14. ul#menu ul li {
  15. float: none;
  16. position: relative;
  17. border-bottom: 1px solid #7EAED7; /* fixes gap problem in IE */
  18. border-left: 1px solid #FFF;
  19. z-index:1000;
  20. }
  21. ul#menu li ul {
  22. margin: 0;
  23. padding: 0;
  24. display:none;
  25. list-style: none;
  26. position: absolute;
  27. background: #9CC;
  28. }
  29. ul#menu ul ul{
  30. margin-left: .2em;
  31. position: absolute;
  32. top: 0; /* if using borders, -1px to align top borders */
  33. left: 100%;
  34. }
  35. ul#menu * a:hover, ul#menu li a:active{
  36. background:#7EAED7 !important;
  37. color: #FFFFFF;
  38. }
  39. ul#menu li a:link,
  40. ul#menu li a:visited,
  41. ul#menu li a:hover,
  42. ul#menu li a:active{
  43. display: block;
  44. padding: .2em .3em;
  45. text-decoration: none;
  46. background: #5587B3;
  47. color: #FFFFFF;
  48. }
  49. ul#menu ul li a:link,
  50. ul#menu ul li a:visited,
  51. ul#menu ul li a:hover,
  52. ul#menu ul li a:active {
  53. width: 8em;
  54. }
  55. Use the following to copy and paste the code.

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

Continue reading. »