Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 );
    }
} 
share|improve this question

2 Answers 2

up vote 0 down vote accepted

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;
}
share|improve this answer
    
why using remove_filter('nav_menu_css_class', 'add_active_class_to_nav_menu'); Only works when I dont use remove filter. –  user1983017 Mar 18 '13 at 0:15
    
remove_filter is to remove the functionality of replacing classes. I added it so that this doesn't affect any of the other menus you might have on your site. As for it not working, make sure you're adding remove_filter line at the bottom of the function –  Mridul Aggarwal Mar 18 '13 at 6:18
    
thanks for explain. I want to override all menu in my site. Thanks –  user1983017 Mar 18 '13 at 7:25
    
In that case, you don't need remove_filter. Also copy the add_filter line from inside the display_element function to directly in your functions.php, otherwise it will not work in case you decide not to use the walker in future. –  Mridul Aggarwal Mar 18 '13 at 8:11

Search for current_page_item. I found it four times total in post-template.php and nav-menu-template.php. Then replace it with active.

share|improve this answer
1  
Please do not encourage people to hack Core files. It leads to nothing but heartache. –  s_ha_dum Jul 26 '13 at 20:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.