Created
February 20, 2018 12:02
-
-
Save anilmeena/ab7b2bf984aa9722713079dea27f9012 to your computer and use it in GitHub Desktop.
Adding different classes to anchor in WordPress navigation menu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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