Posted By

jonatas on 02/13/13


Tagged

wordpress


Versions (?)

WP Menu custom walker


 / Published in: PHP
 

wordpress custom walker to create a menu

  1. /* Navigation Walker
  2. -------------------------------------------------------------------------------*/
  3.  
  4. class premierMenu extends Walker_Nav_Menu
  5. {
  6. function start_el(&$output, $item, $depth, $args)
  7. {
  8. global $wp_query;
  9. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  10.  
  11. $class_names = $value = '';
  12.  
  13. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  14.  
  15. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  16. $class_names = ' class="'. esc_attr( $class_names ) . '"';
  17.  
  18. $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  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. $prepend = '<strong>';
  26. $append = '</strong>';
  27. $description = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';
  28.  
  29. if($depth != 0)
  30. {
  31. $description = $append = $prepend = "";
  32. }
  33.  
  34. $item_output = $args->before;
  35. $item_output .= '<a'. $attributes .'>';
  36. $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
  37. $item_output .= $description.$args->link_after;
  38. $item_output .= '</a>';
  39. $item_output .= $args->after;
  40.  
  41. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  42. }
  43. }

Report this snippet  

You need to login to post a comment.