wp_nav_menu() : custom walker
12,107
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))
Author by
Anthony
Updated on June 04, 2022Comments
-
Anthony almost 2 years
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.
-
Anthony over 10 yearsThat is not really what I want, but you helped me a lot. I also already changed some things on your code to fit my needs, but now I am having another problem on the children elements. Do you mind helping ? Here is my code : snipt.org/Byx0 and snipt.org/Byy8
-
Anthony over 10 yearsThe child are always adding "ui simple dropdown" on their class, while I just want to add "item" for the child.
-
Alex Vauch over 10 yearsMaybe i haven't understood your question... Only child elements should have class "item"? *in your code. if not $args->has_children, then $item_output .= "<div class=\"item\">\n";
-
Anthony over 10 yearsYes that is right, only child elements should have class "item". Like on this code : snipt.org/Byz5
-
Alex Vauch over 10 yearsTry: if (!$args->has_children //not parent && $depth != 0 //play with depth to exclude root elements) $item_output .= "<div class=\"item\">\n";
-
Anthony over 10 yearsOh man it works ! Thanks. I managed myself to do the !$args->has_children , but didn't thought of doing && $depth != 0 ! Thanks a bunch man !