Advertisement
djthoms

Untitled

Oct 5th, 2013
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. class Wpse8170_Menu_Walker extends Walker_Nav_Menu {
  2.  
  3.     var $number = 1;
  4.  
  5.     function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  6.         $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  7.  
  8.         $class_names = $value = '';
  9.  
  10.         $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  11.         $classes[] = 'menu-item-' . $item->ID;
  12.  
  13.         $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  14.         $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  15.  
  16.         $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  17.         $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  18.  
  19.         $output .= $indent . '<li' . $id . $value . $class_names .'>';
  20.  
  21.         // add span with number here
  22.         if ( $depth == 0 ) { // remove if statement if depth check is not required
  23.             $item->title = sprintf( '<span>%02s.</span>', $this->number++ ) . $item->title;
  24.         }
  25.  
  26.         $atts = array();
  27.         $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
  28.         $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
  29.         $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
  30.         $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
  31.  
  32.         $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
  33.  
  34.         $attributes = '';
  35.         foreach ( $atts as $attr => $value ) {
  36.             if ( ! empty( $value ) ) {
  37.                 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  38.                 $attributes .= ' ' . $attr . '="' . $value . '"';
  39.             }
  40.         }
  41.  
  42.         $item_output = $args->before;
  43.         $item_output .= '<a'. $attributes .'>';
  44.         $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  45.         $item_output .= '</a>';
  46.         $item_output .= $args->after;
  47.  
  48.         $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement