Add additional class to WordPress sub-menu item

I needed to target a particular parent menu and add an additional class to the sub-menu ul element. As WordPress builds the menu, you won’t find it in any theme files. Also, I didn’t want to add this globally as the site I was working on was responsive and I shared the same class on the mobile menu too.

So, using jQuery, I added this snippet to my footer so I could target a specific menu’s sub-menu ul and add an additional class to it.

BONUS: As my site also uses a lot of jQuery I added a ‘No Conflict’ mode.

<script>
var $owen = jQuery.noConflict();
$owen("#menu-item-XXX ul.sub-menu").each(function(i){
$owen(this).addClass("mynewclass");
});
</script>

Useful post? Share it

2 comments on “Add additional class to WordPress sub-menu item

  1. Thank you for this nice script. I searched for a solution to highlight the parent menu of my catogory pages, when the user is on a single-post-page. Hardcoded, but it works;))

Leave a Reply

Your email address will not be published.