Remove all Menu Li Classes, Replace with first, last, active
-
I’ve been having a hell of a time removing all of word-presses default menu classes from the li’s while keeping first and last and replacing current-menu-item with active, the standard for css. I have played around with the walker and found it to be far over my head. I am instead using the following custom filter
add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1); function my_css_attributes_filter($var) { //return in_array('current-menu-item', $var) ? $temp = 'active' : ''; if (in_array('current-menu-item', $var)){ $var = 'active'; return $var; }else{ return ''; }
This will replace current-menu-item with ‘active’ when it’s found. This code does not stop the ‘last’ class from being displayed in the last li of the menu which is being displayed by this function
//Adds first and last classes to the first and last menu items function add_first_and_last($output) { $output = preg_replace('/class="menu-item/', 'class="first', $output, 1); $output = substr_replace($output, '<li class="last"', strripos($output, '<li'), strlen('class="menu-item-----"')); return $output; } add_filter('wp_nav_menu', 'add_first_and_last');
To spite this the first li will not show the ‘first’ class when not active. I would simply like the following.
-
<li class=’first active’><!–Shows active too if on current page –>
- <li class=”last”>
I’m not looking for a hand out only to be pointed in the correct direction. What methodology should I go about to try and fix this sense I’m clearly lost. Any help would be greatly appreciated.
- The topic ‘Remove all Menu Li Classes, Replace with first, last, active’ is closed to new replies.