• Hi all,

    I have been working with the WordPress menu walker for sometime and have been able to generate custom HTML.

    I am however unable to customise the drop down menu that will show. Also, the menu has a different style for all drop downs so is it possible to have a walker that will accomplish this.

    The reason I want to be able to do it using the walker is because I am doing it for my client. He wants to be able to edit the menu easily as he is not a coder. A walker fits perfectly for this.

    Looking for quick help.

    Best regards,
    Noman.

Viewing 1 replies (of 1 total)
  • Here is an example of how how to customise the HTML output for menus.

    this will produce menu like:

    <ul>
    <li class="..."><a class="active" href="#">Menu Title</a><li>
    <li class="..."><a href="#">Menu Title</a><li>
    </ul>

    in function.php of your theme:

    class MFC_Walker_Nav_Menu extends Walker_Nav_Menu {
    	/**
    	 * @see Walker::start_el()
    	 * @since 3.0.0
    	 *
    	 * @param string $output Passed by reference. Used to append additional content.
    	 * @param object $item Menu item data object.
    	 * @param int $depth Depth of menu item. Used for padding.
    	 * @param int $current_page Menu item ID.
    	 * @param object $args
    	 */
    	function start_el(&$output, $item, $depth, $args) {
    		global $wp_query;           
    
    		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    
    		$class_names = $value = '';
    
    		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
    		$classes[] = 'menu-item-' . $item->ID;
    
    		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    		$class_names = ' class="' . esc_attr( $class_names ) . '"';
    
    		$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    		$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
    
    		$output .= $indent . '<li' . $id . $value . $class_names .'>';
    
    		$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        ) .'"' : '';
    
                    // new addition for active class on the a tag
                    if(in_array('current-menu-item', $classes)) {
                        $attributes .= ' class="active"';
                    }
    
    		$item_output = $args->before;
    		$item_output .= '<a'. $attributes .'>';
    		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    		$item_output .= '</a>';
    		$item_output .= $args->after;
    
    		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    	}
    }

    then calling it from your page template:

    wp_nav_menu( array('menu_class' => 'menu',
                                        'container' => '',
                                        'container_class' => 'main-menu-container',
                                        'theme_location' => 'primary',
                                        'walker'=> new MFC_Walker_Nav_Menu()
                                        ) );

Viewing 1 replies (of 1 total)
  • The topic ‘Customised Menu Walker’ is closed to new replies.