Use the nav_menu_css_class
filter to add classes to wp_nav_menu
output.
Add ID (no additional query needed):
function wpa_category_nav_class( $classes, $item ){
if( 'category' == $item->object ){
$classes[] = 'menu-category-' . $item->object_id;
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'wpa_category_nav_class', 10, 2 );
Add slug (loads category object via get_category
):
function wpa_category_nav_class( $classes, $item ){
if( 'category' == $item->object ){
$category = get_category( $item->object_id );
$classes[] = 'menu-' . $category->slug;
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'wpa_category_nav_class', 10, 2 );
Related Posts:
- Making breadcrumb with wp_nav_menu
- Unregister Nav Menu from Child-Theme
- Deleted pages are NOT removed from custom menus?
- How to add active class on current menu item page?
- Keeping parent menu items active in child pages
- Add Page ID class to nav menu items
- Way to bulk delete menu items in new 3.x Appearance > Menus system?
- Pass a parameter to a menu walker
- Menu is not visible in appearance
- WordPress nav_menu_link_attributes Not Working
- Customize the menu by adding items
- Nav Menu and display post count
- wp_nav_menu log in/out link?
- Accesibility problems with dropdown menus in twentyten theme or others
- Error “The given object ID is not that of a menu item” on menu with 40+ items
- Theme menu in Admin Panel
- How to target children of child pages
- How to insert custom code into wp_nav_menu structure?
- Adding post excerpt into navigation menu
- Editing WordPress Nav Menu Walker
- WordPress Admin Bar Covering & Overlapping Fixed Menu Header [closed]
- getting parent page id when using custom menu.
- Use menu class in walker function
- Change name & menu_name for Profile in the default menu, w/o altering core files?
- wp-menu ‘automatically add custom taxonomy terms’ like it does for pages? [closed]
- SlickNav – Google Lighthouse Accessibility issue for collapsed menu button
- Creating a navigation menu to see where you are on your website
- Add custom external link to menu
- How I can overlap my menu in my header picture
- current-menu-item class in CSS
- Put class on instead li
- Tiles as a navigation
- Custom format nav menu only using div and a
- How to make the Primary Menu stay the same in the PC and mobile versions of the Twenty Sixteen theme
- How to add notifcations to front-end menu for users when they recieved replies?
- Page content loading issue using Walker_Nav_Menu
- Using Zurb’s Foundation Tabs as WordPress Nav (Active Tab)
- Links do not open from click, only in new tab [closed]
- Default menu editor with automatic page list
- Switch from hover to click on Twenty Twelve Menu
- a better menu, menu options assistance for WP
- Can I have custom post types in “Add menu items” on in the Admin > Appearance > Menu
- Add a custom class to nav li item
- Adding anchors in WordPress Menus
- Adding a unique ID to each sub-menu
- How to add css to single navigation text? [closed]
- add page to main menu (frontend)
- Creating Menu Links in a Custom Theme
- My navigation menu isn’t working. What happened? Please help!
- How to get a multidimensional array of my menu?
- How to get the name of the homepage’s menu?
- How to add search to menu?
- How to get submenu of admin menu?
- wp_nav_menu without menu or theme_location specified
- active menu in post page
- Add third level menu on wordpress
- What is the $menu parameter used of in wp_nav_menu()?
- Blending Foundation’s Tabs with WordPress Nav Menu & Display Active Tab
- Drop down menus with wp_nav_menu or wp_page_menu
- Show wp_nav_menu() based on URL with no fallback
- Editing the nav menu?
- How to display conditional menus with Twenty-twenty two theme (and other new block themes)?
- Keep source page URL parameters on custom link menu item
- I’m trying to remove a duplicate menu, which only appears on the homepage?
- Embedding an external webpage within a page? Is it possible, is it a good idea?
- How to remove empty line space in navigation menu?
- Is it possible to get_the_post_thumbnail (Featured Image) inside a sprintf function?
- Create link/button to create new post using specific template
- WP Menu editor is hiding options for menu items
- Assign a different menu depending on custom taxonomy
- Homepage Menu Link Won’t Function
- Appearance -> Menu editor completely unresponsive
- Custom menu is rendered in all menus
- javascript i wrote not working, can’t figure out why [closed]
- Replacing nav-menus.php file with standard clean one?
- How to add limited items in WordPress menu?
- Making my main menu a hamburger menu on all platforms
- menu change after theme change
- how to create pages and link them to a menu
- menu mobile show up even i in desktop mode in Newspaper Theme
- WordPress fountainpress header is covering top bar
- Outputting Page Title As Tooltip
- Menu subitem doesn’t keep its position
- How to add class and span to
- Appearance > Menus
- extra tag is added in wp_nav_menu
- menu item active class for author.php
- wp_list_pages, links only for pages with no children
- My WordPress menu based on Twitter bootstrap creates menu-item and not page-item
- menu entries are lost
- Additional class for.sub-menu UL?
- Menu item added with “wp_nav_menu_items” gives “damaged” url
- How to call either page name as the nav link or navigation title?
- How to get wordpress navigation to work
- How do I show a different secondary navigation menu for each section of my website?
- How can I make this process automatic?
- How can I alter the visible menu according to which level I am on [closed]
- Site navigation sits unstyled for a second on page reload [closed]
- How to center top Menu bar [closed]
- Changing when mobile menu is displayed