Is this possible?
I want to add different classes to the anchors, so not the same on all anchors. How can I do this?
PS: I am using this:
function add_nav_class($output) {
$output= preg_replace('/<a/', '<a class="your-class"', $output, 1);
return $output;
}
add_filter('wp_nav_menu', 'add_nav_class');
But I want to add different classes to the links…
So like this:
<li><a class="1></a></li>
<li><a class="2></a></li>
and so on…
Yes, it is possible.
You can achieve this using
wp_nav_menu_objects
filter.The only problem is that these classes will be added to
li
elements and not to links directly. But it’s default WordPress behavior and I don’t think you should change it.If you really have to change it, it is still possible:
you can follow the below Steps :
Step 1 : In the wp-admin, go to Appearance -> Menus,put the class on the menu item here “CSS Classes (optional)”. If you can’t see the “CSS Classes (optional)” in the menu item, then there is “Screen Option” in the right top of the screen and there are the options “CSS Class” under “Show advanced menu properties”.
Step 2: Under your them folder/function.php add below code :
Hope above code works for you , let me know if still it doesn’t work.