|
| 1 | +<?php |
| 2 | +// bootstrap 5 wp_nav_menu walker |
| 3 | +class bootstrap_5_wp_nav_menu_walker extends Walker_Nav_menu |
| 4 | +{ |
| 5 | + function start_lvl(&$output, $depth = 0, $args = array()) |
| 6 | + { |
| 7 | + $indent = str_repeat("\t", $depth); |
| 8 | + $submenu = ($depth > 0) ? ' sub-menu' : ''; |
| 9 | + $output .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n"; |
| 10 | + } |
| 11 | + |
| 12 | + function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) |
| 13 | + { |
| 14 | + |
| 15 | + $indent = ($depth) ? str_repeat("\t", $depth) : ''; |
| 16 | + |
| 17 | + $li_attributes = ''; |
| 18 | + $class_names = $value = ''; |
| 19 | + |
| 20 | + $classes = empty($item->classes) ? array() : (array) $item->classes; |
| 21 | + |
| 22 | + $classes[] = ($args->walker->has_children) ? 'dropdown' : ''; |
| 23 | + $classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : ''; |
| 24 | + $classes[] = 'nav-item'; |
| 25 | + $classes[] = 'nav-item-' . $item->ID; |
| 26 | + if ($depth && $args->walker->has_children) { |
| 27 | + $classes[] = 'dropdown-menu dropdown-menu-end'; |
| 28 | + } |
| 29 | + |
| 30 | + $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args)); |
| 31 | + $class_names = ' class="' . esc_attr($class_names) . '"'; |
| 32 | + |
| 33 | + $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args); |
| 34 | + $id = strlen($id) ? ' id="' . esc_attr($id) . '"' : ''; |
| 35 | + |
| 36 | + $output .= $indent . '<li ' . $id . $value . $class_names . $li_attributes . '>'; |
| 37 | + |
| 38 | + $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : ''; |
| 39 | + $attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : ''; |
| 40 | + $attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : ''; |
| 41 | + $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : ''; |
| 42 | + |
| 43 | + $attributes .= ($args->walker->has_children) ? ' class="nav-link dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"' : ' class="nav-link"'; |
| 44 | + |
| 45 | + $item_output = $args->before; |
| 46 | + $item_output .= ($depth > 0) ? '<a class="dropdown-item"' . $attributes . '>' : '<a' . $attributes . '>'; |
| 47 | + $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after; |
| 48 | + $item_output .= '</a>'; |
| 49 | + $item_output .= $args->after; |
| 50 | + |
| 51 | + $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args); |
| 52 | + } |
| 53 | +} |
0 commit comments