• Resolved alexqpe

    (@alexqpe)


    Hello,

    I need to add a class to my first level item, i’ve wrote this code without MaxMegaMenu and it works.

    
    add_filter('nav_menu_css_class', 'add_color_classes', 1, 3);
    function add_color_classes($classes, $item, $args)
        {
            // Main nav only
            if ($args->theme_location == 'main-navigation') {
                // First level only
                if (intval($item->menu_item_parent) === 0) {
                    $page_id = intval($item->object_id);
                    $page_color = get_field('page_accent_color', $page_id)['label'];
                    $classes[] = sanitize_html_class($page_color);
                }
            }
            return $classes;
    }
    

    But when I activate the plugin my class disappear.
    Is there a specific filter/hook to make it works ?

    • This topic was modified 3 years, 10 months ago by alexqpe.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author megamenu

    (@megamenu)

    Hi Alex,

    Are you 100% sure that code works? It doesn’t look right to me. I think you might need to be using this filter https://developer.wordpress.org/reference/hooks/nav_menu_css_class/

    (the same filter also works with MMM, but use it as “megamenu_nav_menu_css_class” instead)

    Regards,
    Tom

    Thread Starter alexqpe

    (@alexqpe)

    Thank You ! Your solution works perfectly !

    Here is my code :

    add_filter('megamenu_nav_menu_css_class', 'add_color_classes', 1, 3);
    function add_color_classes($classes, $item, $args)
        {
            // Main nav only
            if ($args->theme_location == 'main-navigation') {
                // First level only
                if (intval($item->menu_item_parent) === 0) {
                    $page_id = intval($item->object_id);
                    $page_color = get_field('page_accent_color', $page_id)['label'];
                    $classes[] = sanitize_html_class($page_color);
                }
            }
            return $classes;
    }
    • This reply was modified 3 years, 10 months ago by alexqpe.
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Add Class programmatically’ is closed to new replies.