Add class to specific li item menu in function.php

I wish to add a class to a specific item of my primary menu. I'm using this:

function hideMenuItem ( $atts, $item, $args ) {

    $commerce = get_field('ecommerce', 'option');
    if (!$commerce){
        $menu_items = array(566,567, 363, 364 );
        if (in_array($item-ID, $menu_items)) {
           $atts['class'] = 'hidden';
        }

        return $atts;
    }else{

        $menu_items = array(566,567);
        if (in_array($item-ID, $menu_items)) {
           $atts['class'] = 'visible';
        }

        return $atts;
    }
}
add_filter( 'nav_menu_link_attributes', 'hideMenuItem', 10, 3 );

It's working only on the li link but I wish to apply it on the li.

Topic menus Wordpress

Category Web


To add CSS classes to menu item li, use filter nav_menu_css_class (codex).

add_filter('nav_menu_css_class' , 'hideMenuItem' , 15 , 4);

function hideMenuItem($classes, $item, $args, $depth) {

    $commerce = get_field('ecommerce', 'option');
    if ( !$commerce ) {
        $menu_items = array(566, 567, 363, 364 );
        if ( in_array($item->ID, $menu_items) ) {
           $classes[] = 'hidden';
        }
    } 
    else {
        $menu_items = array(566, 567);
        if ( in_array($item->ID, $menu_items) ) {
            $classes[] = 'visible';
        }
    }
    return $classes;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.