Advertisement
MoonWatch

WordPress Menu Walker with div elements

Nov 14th, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. <?php
  2. class Nav_Menu_Div_Walker extends Walker_Nav_Menu {
  3.     private $_submenu_buffer = '';
  4.     private $_clear_buffer = false;
  5.     function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  6.         global $wp_query;
  7.         $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  8.  
  9.         $class_names = $value = '';
  10.  
  11.         $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  12.         $classes[] = 'menu-item-' . $item->ID;
  13.    
  14.         $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  15.         $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  16.  
  17.         $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  18.         $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  19.  
  20.         $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
  21.         $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
  22.         $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
  23.         $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
  24.  
  25.         $item_output = $args->before;
  26.         $item_output .= '<a'. $attributes .'>';
  27.         $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  28.         $item_output .= '</a>';
  29.         $item_output .= $args->after;
  30.  
  31.         if ( $depth > 0 ) {
  32.             $this->_submenu_buffer .= $indent . '<div' . $id . $value . $class_names .'>';
  33.             $this->_submenu_buffer .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  34.         } else {
  35.             $output .= $indent . '<div' . $id . $value . $class_names .'>';
  36.             $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  37.         }
  38.     }
  39.  
  40.     function end_el( &$output, $item, $depth = 0, $args = array() ) {
  41.         if ( $depth > 0 ) {
  42.             $this->_submenu_buffer .= "</div>\n";
  43.         } else {
  44.             $output .= "</div>\n";
  45.         }
  46.         if ( $this->_clear_buffer ) {
  47.             $output .= $this->_submenu_buffer;
  48.             $this->_submenu_buffer = '';
  49.         }
  50.     }
  51.  
  52.     function start_lvl(&$output, $depth, $item) {
  53.         $indent = str_repeat("\t", $depth);
  54.         if ( $depth >= 0 ) {
  55.             $this->_submenu_buffer .= "\n$indent<div class=\"sub-menu\">\n";
  56.         }
  57.     }
  58.     function end_lvl(&$output, $depth) {
  59.         $indent = str_repeat("\t", $depth);
  60.         $this->_submenu_buffer .= "$indent</div>\n";
  61.         if ( $depth == 0 ) {
  62.             $this->_submenu_buffer .= "</div>\n";
  63.             $this->_clear_buffer = true;
  64.         }
  65.     }
  66. }
  67.  
  68. // Example wp_nav_menu() call
  69. wp_nav_menu(array(
  70.     'theme_location' => 'main-menu',
  71.     'walker' => new Nav_Menu_Div_Walker(),
  72.     'items_wrap' => '<div id="%1$s" class="%2$s">%3$s</div>'
  73. )) ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement