|
2 | 2 | // bootstrap 5 wp_nav_menu walker |
3 | 3 | class bootstrap_5_wp_nav_menu_walker extends Walker_Nav_menu |
4 | 4 | { |
| 5 | + private $current_item; |
| 6 | + private $dropdown_menu_alignment_values = [ |
| 7 | + 'dropdown-menu-start', |
| 8 | + 'dropdown-menu-end', |
| 9 | + 'dropdown-menu-sm-start', |
| 10 | + 'dropdown-menu-sm-end', |
| 11 | + 'dropdown-menu-md-start', |
| 12 | + 'dropdown-menu-md-end', |
| 13 | + 'dropdown-menu-lg-start', |
| 14 | + 'dropdown-menu-lg-end', |
| 15 | + 'dropdown-menu-xl-start', |
| 16 | + 'dropdown-menu-xl-end', |
| 17 | + 'dropdown-menu-xxl-start', |
| 18 | + 'dropdown-menu-xxl-end' |
| 19 | + ]; |
| 20 | + |
5 | 21 | function start_lvl(&$output, $depth = 0, $args = array()) |
6 | 22 | { |
| 23 | + $dropdown_menu_class[] = ''; |
| 24 | + foreach($this->current_item->classes as $class) { |
| 25 | + if(in_array($class, $this->dropdown_menu_alignment_values)) { |
| 26 | + $dropdown_menu_class[] = $class; |
| 27 | + } |
| 28 | + } |
7 | 29 | $indent = str_repeat("\t", $depth); |
8 | 30 | $submenu = ($depth > 0) ? ' sub-menu' : ''; |
9 | | - $output .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n"; |
| 31 | + $output .= "\n$indent<ul class=\"dropdown-menu$submenu " . esc_attr(implode(" ",$dropdown_menu_class)) . " depth_$depth\">\n"; |
10 | 32 | } |
11 | 33 |
|
12 | 34 | function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) |
13 | 35 | { |
| 36 | + $this->current_item = $item; |
14 | 37 |
|
15 | 38 | $indent = ($depth) ? str_repeat("\t", $depth) : ''; |
16 | 39 |
|
@@ -40,10 +63,11 @@ function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) |
40 | 63 | $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : ''; |
41 | 64 |
|
42 | 65 | $active_class = ($item->current || $item->current_item_ancestor) ? 'active' : ''; |
43 | | - $attributes .= ($args->walker->has_children) ? ' class="nav-link ' . $active_class . ' dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"' : ' class="nav-link ' . $active_class . '"'; |
| 66 | + $nav_link_class = ( $depth > 0 ) ? 'dropdown-item ' : 'nav-link '; |
| 67 | + $attributes .= ( $args->walker->has_children ) ? ' class="'. $nav_link_class . $active_class . ' dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"' : ' class="'. $nav_link_class . $active_class . '"'; |
44 | 68 |
|
45 | 69 | $item_output = $args->before; |
46 | | - $item_output .= ($depth > 0) ? '<a class="dropdown-item"' . $attributes . '>' : '<a' . $attributes . '>'; |
| 70 | + $item_output .= '<a' . $attributes . '>'; |
47 | 71 | $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after; |
48 | 72 | $item_output .= '</a>'; |
49 | 73 | $item_output .= $args->after; |
|
0 commit comments