1

In order to make a navigation (from scratch), I've been altering the output of the walker. Currently i am trying to find a way to include the 'Optional CCS Class' (found in Appearances -> Menu's in the WP Admin) to the output item menu (start_el). I am starting to doubt if it is possible at this point. Which is why i would like your help. I am using this array to determine if a item has children or is active:

$usedclasses = array(
    (in_array('current-menu-item', $item->classes) ? 'current-menu-item' : ''),
    (in_array('menu-item-has-children', $item->classes) ? 'menu-item-has-children' : ''),
    ($depth % 2 ? 'odd' : 'even')
); $class_names = 'class="' . implode( ' ', $usedclasses ) . '"';

Is there a way to add the custom added classes without having to include all clutter classes? So trying to avoid using:

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

1 Answer 1

1

Not sure you want to do it like this, but just in case, you can use the filter nav_menu_css_class :

function my_special_nav_class( $classes, $item ) {

   if ( is_single() && $item->title == 'Blog' ) {
      $classes[] = 'special-class';
   }

   return $classes;

}

add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );

You'll find more example here

4
  • This certainly is a way to do this, though would force me to add all items i want to use this for manually in the functions.php . I would rather have a way for the Nav_Dynamic_Walker to use the (standard) optional CSS classes that Wordpress provides. This will be a good fallback method if i cannot though. Commented Nov 4, 2016 at 16:25
  • No, you can have an array of the classes you want to add and loop through before returns $classes Commented Nov 4, 2016 at 16:30
  • The only way i seem to be able to apply this is by making an array that goes through the title attributes like this: $optionalclasses = array( ($item->attr_title == 'contact' ? 'contact' : '')); Commented Nov 5, 2016 at 8:19
  • Okay if i am not mistaken it is not possible to get just the optional class from the classes array ($item->classes). Though i found a way around this by adding the title as an class: ( ! empty($item->attr_title) ? $item->attr_title : '') Commented Nov 5, 2016 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.