Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active November 3, 2023 18:03
Show Gist options
  • Select an option

  • Save morgyface/aeea84211991f8e671616d3800647c3c to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/aeea84211991f8e671616d3800647c3c to your computer and use it in GitHub Desktop.
WordPress - Custom menu item and menu link classes using nav filters
@morgyface
Copy link
Author

Add custom classes to wp_nav_menu

This has been a long, lingering frustration of mine; wp_nav_menu allows us to add classes to containers and wrappers, but we have no option to add classes to the items and links within those containers. We're forced to use menu-item and a in our css. Here's where these neat filters come in. Add them to your theme's functions file and then reference them in your templates (typically header or footer includes) as illustrated below in the example.

wp_nav_menu(array(
    'container' => false,
    'theme_location' => 'primary',
    'menu_class' => 'navigation__items',
    'items_wrap' => '<ol class="%2$s">%3$s</ol>',
    'list_item_class' => 'navigation__item',
    'link_class' => 'navigation__item__link'
));

Reference here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment