Advertisement
miriamdepaula

WordPress: Add menu item descriptions

Feb 15th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. <?php
  2. /*
  3. Go to Appearance » Menus. Click on Screen Options button at top right corner of the page. Check the Descriptions box.
  4. (Vá até o menu Aparência » Menus. Clique sobre o botão Opções de Tela, no canto superior direito da página. Marque a opção Descrição)
  5. */
  6.  
  7. class Menu_Description extends Walker_Nav_Menu {
  8.     function start_el(&$output, $item, $depth, $args) {
  9.         global $wp_query;
  10.         $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  11.        
  12.         $class_names = $value = '';
  13.  
  14.         $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  15.  
  16.         $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  17.         $class_names = ' class="' . esc_attr( $class_names ) . '"';
  18.  
  19.         $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  20.  
  21.         $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  22.         $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  23.         $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  24.         $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  25.  
  26.         $item_output = $args->before;
  27.         $item_output .= '<a'. $attributes .'>';
  28.         $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  29.         $item_output .= '<br /><span class="sub">' . $item->description . '</span>';
  30.         $item_output .= '</a>';
  31.         $item_output .= $args->after;
  32.  
  33.         $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  34.     }
  35. }
  36.  
  37. /// Usage (como usar):
  38. $walker = new Menu_Description;
  39. wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'walker' => $walker ) );
  40.  
  41.  
  42. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement