I know you can add a class in the custom menu options, but it adds it to the LI before the A. I want to apply the class directly to this specific A rather then the whole LI.
So instead of the output being
<li id="menu-item-51" class="NEWCLASS menu-item menu-item-type-custom menu-item-51"><a href="#">Link</a> </li>
I want it too be like this.
<li id="menu-item-51" class="menu-item menu-item-type-custom menu-item-51"><a href="#" class="NEWCLASS">Link</a></li>
Any ideas?
you can use
nav_menu_css_class
filterUsing this $item you can any condition you want.
and this will add the class to the specific li and you can style the a tag based on that like so:
I found a solution at this site through the use of a custom walker.
Two steps: replace the default wp_nav_menu code with an edited one, and then add code to the functions.php of the theme.
First, replace the default wp_nav_code with the following (the code is copied from the above site):
Next, add the following code to functions.php. By doing this you can actually add a class to the menu links:
Towards the end of the code are several lines that start with $item_output. In particular, you want to look at this piece:
Because this line determines the output for the beginning of the link html. If you change it to something like this:
Then all your links in the menu will have class=”abc” added to them.
That said, it doesn’t allow a custom class for each link (or at least I don’t know how to code it). This is an issue for me.
For those asking why would you want to do this? I want to have my menu links open lightboxes (colorboxes, to be more specific), and they require classes on the links to do that. For example:
Is there possibly a way to dynamically generate the classes, such as “lightbox1” for the first link, “lightbox2” for the second link, and so on?
SOLVED!!!! I needed to figure this out to make the menu item link to inline HTML in a fancybox. Paste the following code into your theme’s function.php:
Then… in the Menu tab of the WP Dashboard, create a custom link, add it to your menu. On the top where is says Screen Options, make sure you have “Link Relationship (XFN)” checked. It will add that field to your custom menu item. Type “fancybox” (without quotes) into the field and save your menu.
The function looks for the call to the navigation menu, then finds wherever you have a
rel="fancybox"
and replaces it with arel="fancybox" class="fancybox"
. You can replace fancybox with whatever class you need to add to your menu items. Done and done!Current answers don’t seem to recognize that the question is how to add a class to the
a
element and not theli
element, or are too complicated. Here’s a simple approach using thenav_menu_link_attributes
filter that allows you to target a specific menu based on it’s slug and a specific page link in that menu based on it’s ID. You can var_dump $args and $item to get more ways to create your condition.I know this was answered a long time ago, but just as general info, I found how to add a class to each menu item individually using the “Screen” option of the WordPress admin page for Custom Menus.
I had to do something similar recently and figured out another way. I had to add a similar class for a Nivo lightbox plugin. So I added “nivo” to the rel attribute (“Link Relationship (XFN)”) and then the following on the
nav_menu_link_attributes
filter.In the upper area of
Appearance -> Menus
page, click to expand theScreen Options
, check the checkbox ofCSS Classes
. Now when you expand each of the added menu item, you’ll see aCSS Classes (optional)
field.