• Can’t work out why this is putting current-menu-item on every menu item.

    //Filtering a Class in Navigation Menu Item
    add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
    function special_nav_class($classes, $item){
         if(has_term( '23') );{
                 $classes[] = 'current-menu-item';
         }
         return $classes;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • That extra semicolon in the if condition is causing some unexpected parsing. It should be just

    if ( has_term( '23' ) ) {

    Thread Starter powerj

    (@powerj)

    Thank you for your assistance. That solved the problem of the styling being on every menu item but I still seem to have a problem.

    The menu item ‘Our Work’ goes to a page with a portfolio widget. When you select an item(work) from the widget it takes you to a page of the Work (singular post). All the posts have the category of ‘Work’ which is Tag_ID1
    It doesn’t seem to matter what I try I can’t get that page to have the styling for the highlighted menu.
    I’ve tried
    if(has_term(‘work’)
    if(is_category(‘1’)
    if(is_singular(‘post’)

    Can you post the exact code you tried to Pastebin and post the link here? What file did you place that code in?

    Also, depending on what you’re trying to do, you could write some page-specific CSS if your theme uses body_class(). If you know how to use a web inspecting tool like Firebug, you could look at the source code of your page and see if there’s a specific class attached to the <body> tag that you could use.

    Thread Starter powerj

    (@powerj)

    You are a legend.

    Applied it using the page body class.

    Thanks heaps

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add class to nav menu item’ is closed to new replies.