Snippets repository for WordPress developpers

Browse
// functions.php
function clean_nav_menu_classes( $classes ) {
  if( ! is_array( $classes ) )
     return $classes;
  foreach( $classes as $i => $class ) {
    // Remove class with menu item id
    $id = strtok( $class, 'menu-item-' );
    if( 0 < intval( $id ) )
      unset( $classes[ $i ] );
    // Remove menu-item-type-*
    if( false !== strpos( $class, 'menu-item-type-' ) )
       unset( $classes[ $i ] );
    // Remove menu-item-object-*
    if( false !== strpos( $class, 'menu-item-object-' ) )
      unset( $classes[ $i ] );
    // Change page ancestor to menu ancestor
    if( 'current-page-ancestor' == $class ) {
      $classes[] = 'current-menu-ancestor';
      unset( $classes[ $i ] );
    }  
  }

  // Remove submenu class if depth is limited
  if( isset( $args->depth ) && 1 === $args->depth ) {
    $classes = array_diff( $classes, array( 'menu-item-has-children' ) );
  }
  return $classes;
}
add_filter( 'nav_menu_css_class', 'clean_nav_menu_classes', 5 );

Warning: Trying to access array offset on null in /home/clients/7f7a8d8eb6e40c50d3548b06c548734d/web/wp-includes/class-wp-query.php on line 3766

Categories

Tags