wp_nav_menu() : custom walker

14.4k views Asked by At

I am starting at WP theming, and it's been more than one day that I am looking to do the similar menu with the wp_nav_menu function :

      <div class="ui simple dropdown item">
          CATEGORY NAME WITH SUB-CATEGORIES
        <i class="dropdown icon"></i>
          <div class="menu">
            <a href="link_to_sub_category"><div class="item" >Sub Category 1</div></a>
            <a href="link_to_sub_category"><div class="item" >Sub Category 2</div></a>
          </div>
      </div>

Also, if there's no sub categories, the HTML output would be like this :

      <div class="ui simple dropdown item">
          CATEGORY NAME WITHOUT SUB-CATEGORY
      </div>

What do I have to put in my functions.php ? Any hint or any help would be very much appreciated !

Thanks.

1

There are 1 answers

6
Alex Vauch On BEST ANSWER

Add next code to functions.php:

class SH_Nav_Menu_Walker extends Walker {
    var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
    var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );

    function start_lvl(&$output, $depth) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent";
        $output .= "<i class=\"dropdown icon\"></i>\n";
        $output .= "<div class=\"menu\">\n";
    }

    function end_lvl(&$output, $depth) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</div>\n";
    }

    function start_el(&$output, $item, $depth, $args) {
        $value = '';
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes = in_array( 'current-menu-item', $classes ) ? array( 'current-menu-item' ) : array();
        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = strlen( trim( $class_names ) ) > 0 ? ' class="' . esc_attr( $class_names ) . '"' : '';
        $id = apply_filters( 'nav_menu_item_id', '', $item, $args );
        $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
        $item_output = $args->before;
        $item_output .= '<a'. $attributes . $id . $value . $class_names . '>';
        $item_output .= '<div class="item">';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= '</div>';
        $item_output .= "</a>\n";
        $item_output .= $args->after;
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

and call wp_nav_menu in the "right" place with next parameters:

wp_nav_menu(array('items_wrap' => '<div id="%1$s" class="%2$s ui simple dropdown item">%3$s</div>', 'theme_location' => 'sidebar_right_menu', 'walker' => new SH_Nav_Menu_Walker))