Advertisement
Guest User

Untitled

a guest
Jan 27th, 2011
3,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | None | 0 0
  1. class sub_nav_walker extends Walker_Nav_Menu {
  2.     var $found_parents = array();
  3.  
  4.     function start_el(&$output, $item, $depth, $args) {
  5.         global $wp_query;
  6.        
  7.         //this only works for second level sub navigations
  8.         $parent_item_id = 0;
  9.        
  10.         $indent = ($depth) ? str_repeat("\t", $depth) : '';
  11.        
  12.         $class_names = $value = '';
  13.         $classes = empty($item->classes) ? array() : (array) $item->classes;
  14.         $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
  15.         $class_names = ' class="'.esc_attr($class_names).'"';
  16.  
  17.         #current_page_item
  18.         // Checks if the current element is in the current selection
  19.         if (strpos($class_names, 'current-menu-item') || strpos($class_names, 'current-menu-parent') || strpos($class_names, 'current-menu-ancestor') || (is_array($this->found_parents) && in_array($item->menu_item_parent, $this->found_parents))) {
  20.             // Keep track of all selected parents
  21.             $this->found_parents[] = $item->ID;
  22.             //check if the item_parent matches the current item_parent
  23.             if ($item->menu_item_parent != $parent_item_id) {
  24.                 $output .= $indent.'<li'.$class_names.'>';
  25.                
  26.                 $attributes = !empty($item->attr_title) ? ' title="'.esc_attr($item->attr_title).'"' : '';
  27.                 $attributes .= !empty($item->target) ? ' target="'.esc_attr($item->target).'"' : '';
  28.                 $attributes .= !empty($item->xfn) ? ' rel="'.esc_attr($item->xfn).'"' : '';
  29.                 $attributes .= !empty($item->url) ? ' href="'.esc_attr($item->url).'"' : '';
  30.                
  31.                 $item_output = $args->before;
  32.                 $item_output .= '<a'.$attributes.'><span>';
  33.                 $item_output .= $args->link_before.apply_filters('the_title', $item->title, $item->ID).$args->link_after;
  34.                 $item_output .= '</span></a>';
  35.                 $item_output .= $args->after;
  36.             }
  37.             $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
  38.         }
  39.     }
  40.  
  41.     function end_el(&$output, $item, $depth) {
  42.         $parent_item_id = 0;
  43.        
  44.         $class_names = '';
  45.         $classes = empty($item->classes) ? array() : (array) $item->classes;
  46.         $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
  47.         $class_names = ' class="'.esc_attr($class_names).'"';
  48.  
  49.         if (strpos($class_names, 'current-menu-item') || strpos($class_names, 'current-menu-parent') || strpos($class_names, 'current-menu-ancestor') || (is_array($this->found_parents) && in_array($item->menu_item_parent, $this->found_parents))) {
  50.             // Closes only the opened li
  51.             if (is_array($this->found_parents) && in_array($item->ID, $this->found_parents) && $item->menu_item_parent != $parent_item_id) {
  52.                 $output .= "</li>\n";
  53.             }
  54.         }
  55.     }
  56.  
  57.     function end_lvl(&$output, $depth) {
  58.         $indent = str_repeat("\t", $depth);
  59.         // If the sub-menu is empty, strip the opening tag, else closes it
  60.         if (substr($output, -22) == "<ul class=\"sub-menu\">\n") {
  61.             $output = substr($output, 0, strlen($output) - 23);
  62.         } else {
  63.             $output .= "$indent</ul>\n";
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement