WP SITES

3082 Coded Tutorials & 285 Plugins

Add a Custom Class & Style Specific WP Nav Menu Items Conditionally

I found this code on the WordPress Codex recently which i think will be useful.

The code enables you to generate custom classes for specific menu items conditionally.

This way you can use CSS code to change the appearance of each item using conditional tags.

add_filter('nav_menu_css_class' , 'conditional_nav_menu_class' , 10 , 2);
function conditional_nav_menu_class($classes, $item){
if ( is_front_page() AND $item->title == "Home") { 
             $classes[] = "home-icon";
     }
     return $classes;
}

Source https://codex.wordpress.org/Function_Reference/wp_nav_menu

6 responses to “Add a Custom Class & Style Specific WP Nav Menu Items Conditionally”

  1. Changing The Color Of Each Nav Menu Item

    […] Add a Custom Class & Style Specific WP Nav Menu Items Conditionally […]

  2. Tweet Parade (no.2 Jan 2014) – Best Articles of Last Week | gonzoblog

    […] How To Style Specific Menu Items Conditionally – I found this code on the WordPress Codex recently which i think will be useful. The code enables you to generate custom classes for specific menu items conditionally. […]

  3. Hea figurether Hess Avatar
    Hea figurether Hess

    thanks Brad – I think I’ll give it a break for a few days and go for a walk and come back and it will all make sense.

    I am good at adding content to sites and networking but my mind can’t figure coding out very easily.

    Heather

  4. Could you not repeat the code for other pages? If you know for sure the pages you will be using?

    1. Brad Dalton Avatar
      Brad Dalton

      That’s right Scot.

  5. Heather Hess Avatar
    Heather Hess

    thanks Brad – my mind is going crazy with all this coding – I will try and figure it out. However, I would like to get someone to help me so I can network and add content – my two main talents.

    Heather

Leave a Reply

New Plugins