I am currently working on a project where the designer has created 5 top level items in the primary nav of site. These items are meant to look like buttons and have fixed widths, so there will be a problem if the end user of the theme then decides they want 6 top level elements and they retain that fixed width. I needed a way to add a class to the nav defining how many parent/top level items there are, and then I could style the nav based on that.

Here’s what I’ve come up with:

Most examples I found added classes to the nav items themselves and not the container. This was problematic for me because I didn’t want my function firing for every single menu item on the page, only for the nav menus themselves. I finally found the filter I needed wp_nav_menu_args and was able to easily modify the class in the $args for each nav menu as it was being created for the page.

Leave a Reply

Your email address will not be published. Required fields are marked *

+ fifty = 53