add class sub menu wordpress using walker

Posted: September 3, 2014 in web

The easiest way to build a new walker function is to copy the default class (Walker_Nav_Menu) from \wp-includes\nav-menu-template.php and simply customize what you need.

Save it to a new file under you plugin. Look similar to this

class Emas_Nav_Menu extends Walker {
...
	function start_lvl( &$output, $depth = 0, $args = array() ) {
		$indent = str_repeat("\t", $depth);
		$output .= "\n$indent<ul class=\"sub-menu\">\n";
	}

to
	function start_lvl( &$output, $depth = 0, $args = array() ) {
		$indent = str_repeat("\t", $depth);
		$output .= "\n$indent<ul class=\"sub-nav2 nav\">\n";
	}
...

On your plugin just include the new file. require_once(‘walker.php’);

Then you can do this

<?php wp_nav_menu(array('menu' => 'Georgia', 'container' => '', 'menu_class' => 'sub-nav1 nav navbar-nav','walker' => new Emas_Nav_Menu())); ?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s