我一直在寻找解释如何将偶数类和奇数类添加到子菜单项的帖子,但似乎找不到如何将其添加到没有子菜单的主菜单项。
我已经查看了以下链接:
- http://shinraholdings.com/62/custom-nav-menu-walker-function/
- https://css-tricks.com/snippets/php/applying-evenodd-classes/
- https://developer.wordpress.org/reference/functions/wp_nav_menu/#comment-207
- Customizing a walker menu class
- https://wordpress.org/support/topic/odd-class-to-odd-lis-within-sub-menu-uls
- https://wordpress.org/support/topic/add-page-id-to-walker-function-start_lvl
- https://stackoverflow.com/questions/18354343/add-odd-class-to-odd-lis-within-sub-menu-uls-using-custom-walker
- Custom Walker: how to get ID in function start_lvl
- Custom nav walker with different output depending on depth
- Customizing a walker menu class
<?php if ( ! defined( \'ABSPATH\' ) ) exit; // Exit if accessed directly class detailed_walker_nav_menu extends Walker_Nav_Menu { // Add First & Last Classes To Navigational Menu Objects static function first_last_menu_class( $objects, $args ) { // Add first/last classes to nested menu items $ids = array(); $parent_ids = array(); $top_ids = array(); foreach ( $objects as $i => $object ) { // If there is no menu item parent, store the ID and skip over the object if ( 0 == $object->menu_item_parent ) { $top_ids[$i] = $object; continue; } // Add first item class to nested menus if ( ! in_array( $object->menu_item_parent, $ids ) ) { $objects[$i]->classes[] = \'first-menu-item\'; $ids[] = $object->menu_item_parent; } // If we have just added the first menu item class, skip over adding the ID if ( in_array( \'first-menu-item\', $object->classes ) ) continue; // Store the menu parent IDs in an array $parent_ids[$i] = $object->menu_item_parent; } // Remove any duplicate values and pull out the last menu item $sanitized_parent_ids = array_unique( array_reverse( $parent_ids, true ) ); // Loop through the IDs and add the last menu item class to the appropriate objects foreach ( $sanitized_parent_ids as $i => $id ) $objects[$i]->classes[] = \'last-menu-item\'; // Finish it off by adding classes to the top level menu items $objects[1]->classes[] = \'first-menu-item\'; // We can be assured 1 will be the first item in the menu :-) $objects[array_keys( $top_ids )[count( array_keys( $top_ids ) ) - 1]]->classes[] = \'last-menu-item\'; // Return the menu objects return $objects; } // add classes to ul sub-menus function start_lvl( &$output, $depth = 0, $args = array() ) { // depth dependent classes $indent = ( $depth > 0 ? str_repeat( "\\t", $depth ) : \'\' ); // code indent $display_depth = ( $depth + 1 ); // because it counts the first submenu as 0 $classes = array( \'sub-menu toggleable\', ( $display_depth % 2 ? \'menu-odd\' : \'menu-even\' ), ( $display_depth >=2 ? \'sub-sub-menu\' : \'\' ), \'menu-depth-\' . $display_depth ); $class_names = implode( \' \', $classes ); // build html $output .= "\\n" . $indent . \'<ul class="\' . $class_names . \'">\' . "\\n"; } // add main/sub classes to li\'s and links function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { $indent = ( $depth > 0 ? str_repeat( "\\t", $depth ) : \'\' ); // code indent // depth dependent classes $depth_classes = array( ( $depth == 0 ? \'main-menu-item\' : \'sub-menu-item\' ), ( $depth >=2 ? \'sub-sub-menu-item\' : \'\' ), ( $depth % 2 ? \'menu-item-odd\' : \'menu-item-even\' ), \'menu-item-depth-\' . $depth ); $depth_class_names = esc_attr( implode( \' \', $depth_classes ) ); // passed classes $classes = empty( $item->classes ) ? array() : (array) $item->classes; $class_names = esc_attr( implode( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) ) ); // build html $output .= $indent . \'<li id="nav-menu-item-\'. $item->ID . \'" class="\' . $depth_class_names . \' \' . $class_names . \'">\'; // link attributes $attributes = ! empty( $item->attr_title ) ? \' title="\' . esc_attr( $item->attr_title ) .\'"\' : \'\'; $attributes .= ! empty( $item->target ) ? \' target="\' . esc_attr( $item->target ) .\'"\' : \'\'; $attributes .= ! empty( $item->xfn ) ? \' rel="\' . esc_attr( $item->xfn ) .\'"\' : \'\'; $attributes .= ! empty( $item->url ) ? \' href="\' . esc_attr( $item->url ) .\'"\' : \'\'; $attributes .= \' class="menu-link \' . ( $depth > 0 ? \'sub-menu-link\' : \'main-menu-link\' ) . \'"\'; if ( $item->hasChildren ) { $args->after = sprintf( \'<input type="checkbox" id="%1$s-%2$s-checkbox" hidden><label class="toggler" for="%1$s-%2$s-checkbox" onclick><i class="fa fa-lg fa-caret-down"></i></label>\', $item->ID, $args->theme_location ); } else { $args->after = null; } $item_output = sprintf( \'%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s\', $args->before, $attributes, $args->link_before, apply_filters( \'the_title\', $item->title, $item->ID ), $args->link_after, $args->after ); // build html $output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args, $id ); } function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) { // check, whether there are children for the given ID and append it to the element with a (new) ID $element->hasChildren = isset( $children_elements[$element->ID] ) && !empty( $children_elements[$element->ID] ); return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); } } ?>