Skip to content

Instantly share code, notes, and snippets.

@alandbh
Last active September 9, 2017 19:40
Show Gist options
  • Save alandbh/a6655c5a23f2a048a10140171df6734b to your computer and use it in GitHub Desktop.
Save alandbh/a6655c5a23f2a048a10140171df6734b to your computer and use it in GitHub Desktop.
The way WordPress goes through the menu pages to display the items, is using a walker object. In this case the specific class for this object is called Walker_Nav_Menu. You can find it in wp-includes\nav-menu-template.php. The Walker_Nav_Menu is a pretty simple class. You are able to see, how the links and the menu structure are built there. The…
/*
In your functions.php create a class, to extend Walker_Nav_Menu with pretty similar methods to the parent class:
*/
class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_el ( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
// Copy all the start_el code from source, and modify
}
function end_el( &$output, $item, $depth = 0, $args = array() ) {
// Copy all the end_el code from source, and modify
}
}
/*
Then, in your theme files, you can call wp_nav_menu like this:
*/
wp_nav_menu( array(
'theme_location' => 'menu_global',
'container_class' => 'menu_global',
'menu_class' => 'nav side-menu',
'walker' => new Custom_Walker_Nav_Menu,
) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment