hp-nav-walker.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. class navwalker extends Walker_Nav_Menu
  3. {
  4. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  5. $indent = str_repeat("\t", $depth);
  6. $output .= "\n$indent<div class=\"dropdown-menu\">\n";
  7. }
  8. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  9. $indent = str_repeat("\t", $depth);
  10. $output .= "$indent</div>\n";
  11. }
  12. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  13. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  14. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  15. $classes[] = 'menu-item-' . $item->ID;
  16. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
  17. $class_names .= ' nav-item';
  18. if (in_array('menu-item-has-children', $classes)) {
  19. $class_names .= ' dropdown';
  20. }
  21. if (in_array('current-menu-item', $classes)) {
  22. $class_names .= ' active';
  23. }
  24. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  25. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
  26. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  27. if ($depth === 0) {
  28. $output .= $indent . '<li' . $id . $class_names .'>';
  29. }
  30. $atts = array();
  31. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
  32. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  33. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  34. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  35. if ($depth === 0) {
  36. $atts['class'] = 'nav-link';
  37. }
  38. if ($depth === 0 && in_array('menu-item-has-children', $classes)) {
  39. $atts['class'] .= ' dropdown-toggle';
  40. $atts['data-toggle'] = 'dropdown';
  41. }
  42. if ($depth > 0) {
  43. $manual_class = array_values($classes)[0] .' '. 'dropdown-item';
  44. $atts ['class']= $manual_class;
  45. }
  46. if (in_array('current-menu-item', $item->classes)) {
  47. $atts['class'] .= ' active';
  48. }
  49. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
  50. $attributes = '';
  51. foreach ( $atts as $attr => $value ) {
  52. if ( ! empty( $value ) ) {
  53. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  54. $attributes .= ' ' . $attr . '="' . $value . '"';
  55. }
  56. }
  57. $item_output = $args->before;
  58. $item_output .= '<a'. $attributes .'>';
  59. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  60. $item_output .= '</a>';
  61. $item_output .= $args->after;
  62. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  63. }
  64. public function end_el( &$output, $item, $depth = 0, $args = array() ) {
  65. if ($depth === 0) {
  66. $output .= "</li>\n";
  67. }
  68. }
  69. }