replace current_page_item class in menu

I want to replace below class with active class in menu:

.current-menu-item

and

.current_page_item

I am already using a menu callback which add a class dropdown in child menu:

class foundation_navigation extends Walker_Nav_Menu {

function start_lvl(&$output, $depth) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<a href=\"#\" class=\"dropdown-toggle\"><span> </span></a><ul class=\"dropdown\">\n";
}

function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
    $id_field = $this->db_fields['id'];
    if ( !empty( $children_elements[ $element->$id_field ] ) ) {
        $element->classes[] = 'has-dropdown';
    }
        Walker_Nav_Menu::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
    }
} 

2 Answers
2

Add this line at top of your display element function:

add_filter('nav_menu_css_class', 'add_active_class_to_nav_menu');

Add this line at bottom of your display element function:

remove_filter('nav_menu_css_class', 'add_active_class_to_nav_menu');

Add this function somewhere in your themes functions.php:

function add_active_class_to_nav_menu($classes) {
    if (in_array('current-menu-item', $classes, true) || in_array('current_page_item', $classes, true)) {
        $classes = array_diff($classes, array('current-menu-item', 'current_page_item', 'active'));
        $classes[] = 'active';
    }
    return $classes;
}

Leave a Comment