I want to apply a class to a word in my menu. For example, say I have a list item in my menu called “My Item”. I would like to apply a class to the word “Item” so that I can style it anyway I want.
Do I need to add a span to the list item and then add the class to my css like this?
<div id="nav">
<ul>
<li><a href="/MyItem">My <span class="dark-blue">Item</span></a></li>
</ul>
</div>
#nav ul li a.dark-blue {
color: #00008B;
}
You can filter the
title
of each nav menu item. Unfortunately, the filter is named'the_title'
â if we just add a simple filter for it, we may catch strings outside of nav menus too.So we need a second function to switch our nav menu title filter on when the menu class starts and off when it ends.
Letâs start with the second function:
'wp_nav_menu_args'
is called inwp_nav_menu()
very early. Whenwpse_14037_filter_switch()
is running in this context (current_filter
) we turn the title filter on.'wp_nav_menu'
is called after all item are rendered. Now we turn the title filter off.Now we need the
wpse_14037_filter_title()
we just registered:I decided to make the function more generic than you wanted it. Other people may want to style the third word ⦠or the 15th.
To address the second word in your stylesheet just use the new class: