I’m working on a custom theme using wp_nav_menu(). What I want to do is add a caret to menu items that have sub-menus. For example, If my menu looks like this:
- Menu Item 1
- Menu Item 2
- Menu Item 2a
- Menu Item 2b
- Menu Item 3
I want to be able to format it like this:
- List item
- Menu Item 1
- Menu Item 2 >
- Menu Item 2a
- Menu Item 2b
- Menu Item 3
Without knowing the structure of the menu. This seems like a pretty common formatting problem, so I was wondering if there is any built-in functionality to provide for this.
I do this using jQuery (since it doesn’t necessarily need to be in the TEXT (for screen readers, etc.) – just another option…:
Then for that “parentul” class I put in a background image of an arrow and position it to the right > …
You can do this using a custom walker. Paste the following class at the bottom of your functions.php:
And then in header.php (or whever your wp_nav_menu is) do something like this:
The key part of the walker is the following if statement:
This checks if the item has children, and if it does it adds the ‘parent’ css class to it, and then changes the title from ‘xxxx’ to ‘xxxx >’.
(Adapted from http://wordpress.org/support/topic/wp_nav_menu-add-a-parent-class)
These days it looks as if WordPress itself outputs a
menu-item-has-children
class on a menuli
that has aul.sub-menu
.One can easily add styles to this to add a background image rather than having to use jQuery or the like.