I use this functions. First of all, you have to add some custom class to your menu item (allow class input in Screen options, it’s not visible by default).
function mark_menu_item_as_active($classes, $item) {
if( in_array('my-custom-class',$classes) && ( is_category('my-category') /* OR ...*/ ) ) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter('nav_menu_css_class', 'mark_menu_item_as_active', 10, 2);
This function iterates through all menu items, and you will find your target item with that custom menu class. Then you will check whatever conditions you have (is_category, …), and add another class to its classes (current-menu-item
, …).
Related Posts:
- What Defines What Category A Post Picks (if in multiple)
- WordPress menu that shows all posts in a certain category like w3schools
- Exclude category from
- How to list recent posts in a wp nav menu?
- Disable sticky option for specific categories
- Redirect to another page using contact form 7? [closed]
- Is there a way to add the list of recent posts into the admin sub menu on hover?
- how to hide empty fields of post category description?
- Give posts a particular template based on the assigned category
- Un-highlight Blog Menu Item when Category Menu Item is Selected
- Programmatically add posts from specific category to menu
- wp_list_categories() Exclude All Categories Except One
- WordPress – When visiting a `single post/ single.php` my category menu item link becomes active
- I want to add the 10 most recent posts under a parent category on the main menu
- How to add class to specific navbar item when post parent category is in specific category
- Automatic Table of contents with categories and posts
- Change from all posts to specific categories post on main page?
- How to remove category and other tags from posts page
- How to get the number of posts in a selected category?
- How to add post count to wp_nav_menu?
- How to know if get_posts() failed?
- Show related posts by category but ignore one category
- Add custom field to category of custom post type
- Can I hide a specific post from latest posts page?
- How to show post(excerpt) from specific category on wp page?
- How do I create an archive page for standard posts?
- WordPress – Admin Manage Posts – Multiple Filters by Parent Category
- Custom Theme Want To Remove One Category From All Displays
- Add Product categories to WordPress menu without losing hierarchy
- Divide Loop Into Days & Categories
- How to load a different header based on post format?
- Different template for posts of all subcategories of category
- Understand post type
- How to filter posts that belong to a specific category only if that is the only category
- Category List (Archive) Page Template By Category
- Should I use custom menu, C.P.T. or theme options, or something else for this?
- List posts in a given category
- Why orderby=”date” does not support manually changed posts?
- page.php showing loop of posts and not page content
- Child_of not displaying all children posts.. via get_posts
- post categories
- How to create a submenu that will navigate through different posts in the same page
- How to display the link (title) and thumbnail post?
- How can i limit the number of posts created per category?
- Get Posts Under Custom Taxonomy
- Prevent additional top level categories but allow more children
- category permalinks
- How can I hide tags on a child-category page, if that tag has not been used?
- How do I stop the loop from repeating in my category template?
- Hide posts belongs to few categories in homepage
- post category in wp_insert_post
- disable Tab post on nav-menus page (Admin)
- What does WordPress do if I save a post without content/title? [duplicate]
- store posts_id of category into a varable?
- Display posts of specific category term
- Widget that shows categories with posts numbers
- Latest posts by category — how to exclude current post?
- Relative number of post in category
- How to get post with associated categories and tags names instead of ids with rest api?
- Display a mixed list of posts and media
- Cant’ Grab WordPress Built-in Posts Through Loop
- Remove Featured Image from posts in specific category?
- Sidebar limiting to 10 posts?
- WordPress Architecture as a CMS – Posts & Pages
- How to list post as buch of category, and all of them
- How can I add a page that shows posts from a single category?
- Archives and Categories in Custom Menu
- Post in bulk based on category template
- Remove subcategory slug from url
- Add a field in my rest api
- Can I show category name in url for only one of my categories?
- Automatic duplicate/copy of post to a related page in WordPress
- Is there a way to categorize a list posts all at once?
- Yoast breadcrumb URL shows category instead of the page
- Menu Items disappearing/being empty on save or post edit
- On the online version of my blog posts disappear
- Current post categories and subcatecories outside of the loop
- Theme: dropdown hover menu not showing up in IE/Edge
- Query prints posts without specific categories
- Foreach issue in get_posts function
- More Than 50K Categories and WordPress Admin Panel Stop Showing Categories and Posts
- Re-order Category Meta-data
- Custom post styling per category?
- post categories – how to show only categories with a specific parent id
- Creating a “category” of featured posts
- How do I make it so that new posts within a certain category go into a certain page?
- Posts only showing on /localhost/wordpress/ and not on other pages
- List posts of assigned categories in list of all categories on single.php
- Add custom class to existing menu items from custom meta
- How to show the posts list into a static page? Problems to use the loop into a static page
- Assign specific ads to specific categories/posts
- find posts with exactly 3/4 categories
- Use radio buttons & checkboxes for post categories
- wordpress taxonomy results
- Displaying only posts from a certain category on my custom page template
- WP_Query: Fetch posts that are in (category1 and not in category2), OR posts that are not in cagegory1
- Add default content to post (for specific category)
- List posts that are in the same category as the current post title
- The post order is different for logged-in and non-logged-in users? [closed]
- How can I get on the same row two post of different categories?