There are several built-in CSS classes that come with the wp_nav_menu
.
In your case, you are looking for something like a current category class, which is not included. But we can easily add that to the CSS classes:
function wpse_134409_current_category_class($classes, $item) {
if (
is_single()
&& 'category' === $item->object
&& in_array($item->object_id, wp_get_post_categories($GLOBALS['post']->ID))
)
$classes[] = 'current-category';
return $classes;
} // function wpse_134409_current_category_class
add_filter('nav_menu_css_class', 'wpse_134409_current_category_class', 10, 2);
Please note: this code is untested!
Now you can target your menu item like so:
#menu-id li.current-category {
/* styles */
}
Related Posts:
- Multiple menu items highlighted
- Open subcategories in submenu on click in primary menu
- Create menu / submenu from category subcategory and posts
- Give Children Unique ID’s
- Multiple categories for one menu item?
- Global navigation in multisite: problem with categories
- How to add all subcategories as submenu in WordPress menu
- omit categories from main navigation menu
- How to create an automatic MultiColoumn MegaMenu with Categories WordPress
- Get the children of the parent category
- wp_nav_menu not appearing for a couple pages
- Main menu not appearing on category pages
- Custom Nav Walker menu – Display category count
- Reflect nested categories in appearance > menus
- Category menu item and its last 10 posts as sub-menu
- Is there a function to cause empty categories not to show in menus?
- Assign different category colours to different categories in the main menu
- Keep active class in post page
- Auto add pages to category menu
- How to show empty category in admin menus search
- Add custom taxonomy terms to WordPress menu dynamically & append #slug to url
- Display WooCommerce product categories in a 4 columns custom menu [closed]
- Is it possible to put next and previous category links?
- Automatically add product_categories to nav_menu
- Previous/next link to post in category, NOT tag?
- Add Product categories to WordPress menu without losing hierarchy
- Custom Category/Subcategory structure
- Add a specific category at a specific place to the menu that uses wp_list_pages
- Show recent posts in menu?
- Show Different Custom Menu on Different Category Pages
- How to add Categories to the top menu of the website?
- Implementing a tricky wordpress menu (nested categories + thumbnails)
- Looking for the best solution to build this Blog structure? Picture attached?
- Nav menu category links not showing
- How to display only posts assigned to a particular, isolated, subcategory
- Show number of articles in menu
- How to show only one category in breadcrumb navigation
- Automatically adding categories and authors in custom menu?
- Hard coded main navigation
- Highlight wp_nav_menu when category is selected
- Checkboxes in the category list?
- Class active for Archive link – looped out
- Restoring default article pagination on archives – Removing custom limits
- previous/next_post_link in the same sub-category?
- Menus don’t work after 4.4 [closed]
- category permalinks
- Categories as main menu items and subcategories as nested lists
- wp_list_categories in in nav_menu
- Different menu navigation per category
- WP admin broken. Not saving catgories or adding categories to menu
- Creating Category Specific menus
- Custom Walker for wp_list_categories
- Create menu locations for each category in wordpress
- How to display numbered pages in a category
- Building Menu from Woocommerce Product Categories
- Hooking to walker_nav_menu_start_el to insert list of subpages
- Show subcategories on a parent category page
- Admin: Navigation Menus do not show empty categories/taxonomies in the list’s search tab
- Change div background acording to the selected category
- WordPress list categories dropdown with parent-child relation and child under it’s parent
- List a maximum of ‘x’ product categories with qty of ‘y’ in each column
- Searching for a category returns nothing if category is empty
- function query_posts disabling current_page_menu class
- Show different menu on each woocommerce category page
- Theme for subcategories
- Same menu for different taxonomies to reach different content
- How to get category id’s which are added in main menu?
- How to Sync Menu, Widgets and other masters from Main Website to its Sub Site
- Link from navbar to categories
- Pagination doesn’t work on multiple categories
- Theme modification for custom category menus
- Change appearance based on category but post is in two main categories
- wp_nav_menu not appearing correctly on category page
- wp_list_pages() not showing on posts
- Is custom menu name and category name linked in wordpress?
- Custom menu with categories and tags
- How to automatically generate custom menu item from categories?
- WP-CLI Add category as menu item main-menu
- display specific category when website loads
- Categories are not available AT ALL under Menus
- ADD DYNAMIC attribute for category to accordion
- How to remove an item from a custom Walker_Nav_Menu
- Display menu in category and sub category
- WordPress category title not update in navigation menu
- Function add class to second menu specific items if the page has a certain category
- Dynamically display sub-categories on primary menu without adding them
- Show alternative menu based on page template AND post category
- Menu with category 2 category combined
- Next and previous links category infinite loop
- Tag Menu items based on their categories
- Loop within category’s posts
- Backend – Categories and Menus conflicts
- Hide category links if there is nothing in the categories – Main Navigation submenu item
- Grouping parent categories into sections
- Using categories and pages in menus
- A simple question regarding tags and categories
- get_adjacent_post_link excluded_terms ignored
- Category menu that filters out empty categories
- Transfering static site to wordpress and retaining category by month paginations
- Conditions for single post