Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. <?php
  2. class BootstrapNavMenuWalker extends Walker_Nav_Menu {
  3.  
  4. function start_lvl(&$output, $depth = 0, $args = Array()) {
  5.  
  6. $indent = str_repeat("\t", $depth);
  7. $submenu = ($depth > 0) ? ' sub-menu' : '';
  8. $output .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n";
  9. }
  10.  
  11. function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
  12.  
  13. $indent = ($depth) ? str_repeat("\t", $depth) : '';
  14.  
  15. $li_attributes = '';
  16. $value = '';
  17.  
  18. $classes = empty($item->classes) ? array() : (array) $item->classes;
  19.  
  20. // managing divider: add divider class to an element to get a divider before it.
  21. $divider_class_position = array_search('divider', $classes);
  22. if ($divider_class_position !== false) {
  23. $output .= "<li class=\"divider\"></li>\n";
  24. unset($classes[$divider_class_position]);
  25. }
  26.  
  27. $classes[] = ($args->has_children) ? 'dropdown' : '';
  28. $classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
  29. $classes[] = 'menu-item-' . $item->ID;
  30. if ($depth && $args->has_children) {
  31. $classes[] = 'dropdown-submenu';
  32. }
  33.  
  34. $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
  35. $class_names = ' class="' . esc_attr($class_names) . '"';
  36.  
  37. $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
  38. $id = strlen($id) ? ' id="' . esc_attr($id) . '"' : '';
  39.  
  40. $output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
  41.  
  42. $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
  43. $attributes .=!empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
  44. $attributes .=!empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
  45. $attributes .=!empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';
  46. $attributes .= ($args->has_children) ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';
  47.  
  48. $item_output = $args->before;
  49. $item_output .= '<a' . $attributes . '>';
  50. $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
  51. $item_output .= ($depth == 0 && $args->has_children) ? ' <b class="caret"></b></a>' : '</a>';
  52. $item_output .= $args->after;
  53.  
  54. $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
  55. }
  56.  
  57. function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
  58. //v($element);
  59. if (!$element)
  60. return;
  61.  
  62. $id_field = $this->db_fields['id'];
  63.  
  64. //display this element
  65. if (is_array($args[0]))
  66. $args[0]['has_children'] = !empty($children_elements[$element->$id_field]);
  67. else if (is_object($args[0]))
  68. $args[0]->has_children = !empty($children_elements[$element->$id_field]);
  69. $cb_args = array_merge(array(&$output, $element, $depth), $args);
  70. call_user_func_array(array(&$this, 'start_el'), $cb_args);
  71.  
  72. $id = $element->$id_field;
  73.  
  74. // descend only when the depth is right and there are childrens for this element
  75. if (($max_depth == 0 || $max_depth > $depth + 1) && isset($children_elements[$id])) {
  76.  
  77. foreach ($children_elements[$id] as $child) {
  78.  
  79. if (!isset($newlevel)) {
  80. $newlevel = true;
  81. //start the child delimiter
  82. $cb_args = array_merge(array(&$output, $depth), $args);
  83. call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
  84. }
  85. $this->display_element($child, $children_elements, $max_depth, $depth + 1, $args, $output);
  86. }
  87. unset($children_elements[$id]);
  88. }
  89.  
  90. if (isset($newlevel) && $newlevel) {
  91. //end the child delimiter
  92. $cb_args = array_merge(array(&$output, $depth), $args);
  93. call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
  94. }
  95.  
  96. //end this element
  97. $cb_args = array_merge(array(&$output, $element, $depth), $args);
  98. call_user_func_array(array(&$this, 'end_el'), $cb_args);
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement