Advertisement
Guest User

wp_nav_menu walker (sub menus of current level 0 item)

a guest
Apr 20th, 2011
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.55 KB | None | 0 0
  1. class Kanec_Walker_Nav_Menu extends Walker {
  2.     /**
  3.      * @see Walker::$tree_type
  4.      * @since 3.0.0
  5.      * @var string
  6.      */
  7.     var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
  8.  
  9.     /**
  10.      * @see Walker::$db_fields
  11.      * @since 3.0.0
  12.      * @todo Decouple this.
  13.      * @var array
  14.      */
  15.     var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
  16.    
  17.     /**
  18.      * @see Walker::start_lvl()
  19.      * @since 3.0.0
  20.      *
  21.      * @param string $output Passed by reference. Used to append additional content.
  22.      * @param int $depth Depth of page. Used for padding.
  23.      */
  24.     function start_lvl(&$output, $depth) {
  25.         $indent = str_repeat("\t", $depth);
  26.         $output .= "\n$indent<ul class=\"sub-menu\">\n";
  27.     }
  28.  
  29.     /**
  30.      * @see Walker::end_lvl()
  31.      * @since 3.0.0
  32.      *
  33.      * @param string $output Passed by reference. Used to append additional content.
  34.      * @param int $depth Depth of page. Used for padding.
  35.      */
  36.     function end_lvl(&$output, $depth) {
  37.         global $current_branch;
  38.         if ($depth == 0) $current_branch = false;
  39.         $indent = str_repeat("\t", $depth);
  40.         $output .= "$indent</ul>\n";
  41.     }
  42.  
  43.     /**
  44.      * @see Walker::start_el()
  45.      * @since 3.0.0
  46.      *
  47.      * @param string $output Passed by reference. Used to append additional content.
  48.      * @param object $item Menu item data object.
  49.      * @param int $depth Depth of menu item. Used for padding.
  50.      * @param int $current_page Menu item ID.
  51.      * @param object $args
  52.      */
  53.     function start_el(&$output, $item, $depth, $args) {
  54.         global $wp_query;
  55.         global $current_branch;
  56.        
  57.         // Is this menu item in the current branch?
  58.         if(in_array('current-menu-ancestor',$item->classes) ||
  59.         in_array('current-menu-parent',$item->classes) ||
  60.         in_array('current-menu-item',$item->classes)) {
  61.             $current_branch = true;
  62.         }
  63.        
  64.         if($current_branch && $depth > 0) {
  65.             $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  66.  
  67.             $class_names = $value = '';
  68.  
  69.             $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  70.             $classes[] = 'menu-item-' . $item->ID;
  71.  
  72.             $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  73.             $class_names = ' class="' . esc_attr( $class_names ) . '"';
  74.  
  75.             $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  76.             $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
  77.  
  78.             $output .= $indent . '<li' . $id . $value . $class_names .'>';
  79.  
  80.             $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
  81.             $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
  82.             $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
  83.             $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
  84.  
  85.             $item_output = $args->before;
  86.             $item_output .= '<a'. $attributes .'>';
  87.             $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  88.             $item_output .= '</a>';
  89.             $item_output .= $args->after;
  90.        
  91.             $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  92.         }
  93.  
  94.     }
  95.  
  96.     /**
  97.      * @see Walker::end_el()
  98.      * @since 3.0.0
  99.      *
  100.      * @param string $output Passed by reference. Used to append additional content.
  101.      * @param object $item Page data object. Not used.
  102.      * @param int $depth Depth of page. Not Used.
  103.      */
  104.     function end_el(&$output, $item, $depth) {
  105.         global $current_branch;
  106.         if($current_branch && $depth > 0) $output .= "</li>\n";
  107.         if($depth == 0) $current_branch = 0;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement