Skip to content

Instantly share code, notes, and snippets.

@anilmeena
Created February 20, 2018 12:02
Show Gist options
  • Save anilmeena/ab7b2bf984aa9722713079dea27f9012 to your computer and use it in GitHub Desktop.
Save anilmeena/ab7b2bf984aa9722713079dea27f9012 to your computer and use it in GitHub Desktop.
Adding different classes to anchor in WordPress navigation menu
<?php
/* Add this code to your theme's functions.php file */
function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
// you can put your if statements in here (use item, depth and args in conditions)
if ( $depth == 1 ) {
$item_output = preg_replace('/<a /', '<a class="level-1-menu" ', $item_output, 1);
} else if ( $depth == 2 )
$item_output = preg_replace('/<a /', '<a class="level-2-menu" ', $item_output, 1);
}
// .. and so on
return $item_output;
}
add_filter('walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment