Customizing the new WordPress 3.0 Custom Menus Adding Different Icons to Each Link

    I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot of different options to play with, let’s see what we’ll do.

    1. What we’re going to do?

    Is very simple, I want a menu with each link containing different icons:

    2. WordPress Custom Menus

    WordPress 3.0 Custom Menus are a simple but effective new feature of the latest beta version of WordPress. To know more about this new feature read here.

    3. The Problem

    I need to assign differents CSS classes to each link to add my icons.

    4. The Solution

    Let’s use the Title Attribute and add it as classes using jQuery to each menu link.

    [Update:]
    There’s some hidden features in the WordPress Custom Menu and thanks to John O’Nolan I just notice those features, there’s no need of jQuery here, we just need to click in Screen Options to show the hidden options.

    You’ll need to specify the name of each individual class using:

    • Home
    • Advertise
    • Contact
    • Archives
    • About

    5. The CSS

    This is just some generic styles to show you how I’ve named my links.

    #header ul.navigation li.home {background:url("images/home.png") no-repeat 0 1px transparent;display:block;padding-left:23px}
    #header ul.navigation li.home:hover {background-position:0 -16px;color:#535353;border-bottom:1px dotted #535353;}
    
    #header ul.navigation li.about {background:url("images/about.png") no-repeat 0 0px transparent;display:block;padding-left:23px}
    #header ul.navigation li.about:hover {background-position:0 -16px;color:#535353;border-bottom:1px dotted #535353;}
    
    #header ul.navigation li.advertise {background:url("images/advertise.png") no-repeat 0 0px transparent;display:block;padding-left:23px}
    #header ul.navigation li.advertise:hover {background-position:0 -17px;color:#535353;border-bottom:1px dotted #535353;}
    
    #header ul.navigation li.archive {background:url("images/archive.png") no-repeat 1px 2px transparent;display:block;padding-left:23px}
    #header ul.navigation li.archive:hover {background-position:1px -14px;color:#535353;border-bottom:1px dotted #535353;}
    
    #header ul.navigation li.contact {background:url("images/contact.png") no-repeat 0 3px transparent;display:block;padding-left:23px}
    #header ul.navigation li.contact:hover {background-position:0 -12px;color:#535353;border-bottom:1px dotted #535353;}
    

    6. jQuery in Action

    The solution is pretty simple, we just grab the Title Attribute and add it to the links as a class using the addClass jQuery function.

    There’s no need of jQuery to add individual classes, but I’ll leave this code here for reference.

    <script type="text/javascript">
        $(document).ready(function(){
             $('ul.navigation li').addClass(function() { return $(this).attr('title'); });
        });
    </script>
    

    7. That’s it

    Thanks to John O’Nolan for the heads up.

Discussion

  1. 5-Squared says:

    Great post Ivor! I haven’t started playing with 3.0 yet, but looking forward to it!

    Cheers.

  2. This is great for post-installation where someones creating a menu sure, but if you’re creating a theme you need to remember to include something for the ‘default’ that comes up as well or your site’s going to look horrible to those users who get your theme.

    But yeah, nice.

  3. JohnONolan says:

    Or, click on “screen options” and enable “CSS classes” http://drp.ly/14iT6Q

    ;)

  4. I don’t get it. WordPress 2 already gives you unique classes to do icons with in the navigation. It’s how I did the nave on my site….

  5. oronm says:

    amazing!! can’t believe i missed it! thanks

  6. Lilia says:

    Thanks so much for this! Been trying to customize external links in the menu for days. I was able to specify classes in beta2, but when they released RC1/RC2 I thought they canned the ability. Thanks to you I adjusted my screen options, and voila!

  7. Víctor says:

    I don´t know where to put the CSS code. I tried in style.css, but it doesn’t work.

    Can anyone help me?

  8. Bill says:

    Thanks for this post. I wanted to have the last two items on my menu float right, as opposed to left, which is the default. I added a css class “floatright” to the “Contact” page, using the WP 3.0 menu function and added this line to style. css:

    .floatright {float: right}

    It doesn’t have the intended impact and when I view source I see this:

    Contact

    What is menu-item, menu-item-type-post, etc.

    Any thoughts?

    Thanks, Bill

  9. Bill says:

    Rats, I see that it didn’t show you the code, but created a link, so here it is, fudged a bit so it doesn’t create a link

    Contact

  10. Bill says:

    trying one more time

    Contact

  11. maria porto says:

    what about an active state?

  12. Gabriel Tadeu says:

    Do you know how ‘description’ works?

  13. Marc says:

    Hello Ivor,
    Great article!
    however I cant seem to figure out where I put the CSS in order for this to work.
    any help would be greatly appreciated.
    Thank you

    • Josh says:

      I have exactly the same problem. Could you please update that part of your post because it’s somewhat confusing. I’m trying to do specifically that – yet this is the only tut I’ve found after 4 hours of searching….

  14. Lisa says:

    Thank you so much for this. I was having such a problem customizing the navigation and you made it so much more simple.

    I couldn’t find any information on this.

    Thanks again.

  15. Chantelle says:

    Hi, Can the new “custom menu” be customized further to make it a dropdosn menu?

  16. Ændrew says:

    Thanks a ton, the note about “Screen Options” just made my day. :)

  17. Hey Ivor, just to let you know this post has become my bible.

    Have been scratching my head off to find a tutorial that explains how to edit WordPress custom menus.

  18. Rahul Anand says:

    Really Nice Trick to customize the Menus. Thanks John O’Nolan.

  19. Thanks for this post. I entirely agree with what you are saying. I have been talking about this subject a lot lately with my father so may this will get him to see my point of view. Fingers crossed!

  20. I’ve by no means heard of this just before. I’ll definately have to examine individuals out.

  21. I gotta bookmark this site it seems invaluable handy

  22. Anton says:

    Great tutorial, I can’t figure out couple things. Can I get class ‘home’ to be added to anchor instead of li element:

    from this:

    Home

    to this:

    Home

    Thank you.

    • Anton says:

      Great tutorial, I can’t figure out couple things. Can I get class ‘home’ to be added to anchor instead of li element:

      from this:

      Home

      to this:

      Home

      Thank you.

  23. Samrat says:

    This is a short tutorial on wordpress menus. It might be helpful:-
    add menus to wordpress

  24. Deepan says:

    This post is more helpful for me. Thanks for posting. Good work

  25. Ryan says:

    Wow, I can’t believe I missed that in Screen Options, haha. Thanks for the article!

    I am currently trying to work out a way to apply the class to the actual link element. I’d like to have a menu item open in a lightbox, but I am having trouble doing so. Any ideas?

  26. This article was a great help it allowed me to add a awesome home button to my website http://www.speedsitting.co.nz check it out if you like it see the source code to see how I achieved it.

    Hint: Background Images and a bit of CSS Trickery.
    Note the links anchor text too, great way for SEO.

  27. Very nice post. I just stumbled upon your blog and wanted to say that I have truly enjoyed browsing your blog posts. After all I will be subscribing to your rss feed and I hope you write again soon!

  28. rc says:

    When I try this, WordPress is stripping out the CSS meta characters and it’s not working, so this:

    #menu-item-1256 {background url:(image.gif);}

    …becomes:

    menu-item-1256 backgroundurlimagegif

  29. Thanks a lot, Ivor. I came to your site through a Google search looking for a way to place images in the menu, and this is exactly what I needed for my website. (You can have a look at it).

    It looks fabulous – thanks a ton!

  1. Gadget Newz linked here

    [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  2. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  3. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  4. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  5. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  6. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  7. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  8. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  9. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  10. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  11. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  12. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  13. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  14. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  15. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  16. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  17. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  18. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  19. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  20. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  21. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  22. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  23. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  24. Todo linked here

    [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

  25. [... I came across with an issue recently, I was trying to add different classes to each menu link without any success. The new WordPress Custom Menu feature have a lot ...]

Leave a Comment