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
- Show category name in category.php when posts assigned to multiple categories
- Show posts assigned to multiple categories in current category page
- Menu for all posts from multiple categories
- WordPress menu disappears in category pages
- Menu item on a category
- Duplicate posts and change category
- Should I use posts or pages in this scenario?
- Category Foreach keeps looping?
- Using permalinks, category slugs, and tag slugs
- post categories
- How to limit posts by category
- Blog page not working
- Is it possible to change post id for an already added menu item in WordPress?
- Assign category to new post via URL
- How to create a submenu that will navigate through different posts in the same page
- Stop users posting into category while still seeing contents of the category
- How to display the link (title) and thumbnail post?
- Underscores Theme Unit Testing – Catching Untitled Posts
- get_the_excerpt() is not working as expected – returns wrong text
- Category Page Template not Showing Posts
- display a random post thumbnail from a random category
- How do I retrieve then print the currently viewed sub categories?
- How to use format post in a pertinent way
- How to allow users to post only in certain category and hide elements from edit page?
- More then one menu items are assigned with “current-menu-item” class
- How can i limit the number of posts created per category?
- How to Restrict Previous & Next Post Link to Posts of Same Category?
- Pulling in post category and children within category
- WordPress custom Query for Posts in Category display posts multiple times
- Post categories to pages
- Get user categories with most posts in it
- One menu for frontpage (one pager), and a different menu for posts
- Unable to restore from backup – how to obtain old blog posts?
- Post page to display specific category
- Get Posts Under Custom Taxonomy
- Get attachments for posts that belongs to a specific category
- Prevent additional top level categories but allow more children
- Need help with category listing!
- category permalinks
- Prepending %category% onto default posts fails
- List all categories with featured image from post?
- Categories list into registration form
- WP_Query: Mixing category__in and tag__in together
- If in_category not working for multiple single.php pages
- How can I setup a relationship using categories in WordPress?
- WordPress Multisite – Create Default Post and New Category On New Site Install
- how to show single post in a custom template
- Using ajax on editing a category edit page?
- Related posts by category not working right
- Set Default Category to Username
- Dynamic dependent Dropdown lists for categories, sub-categories and posts
- How can I hide tags on a child-category page, if that tag has not been used?
- date issue with category post retrival
- Listing Specific Categories from Current Post with Depth
- How to make multiple sections in home pulling posts category wise?
- Highlight static blog page link in header
- How to display two blog categories as separate sections on one page?
- How to make the first post in the loop be styled like a “new / featured” post?
- How to Mysql select a list of posts with meta_values AND all relevant categories?
- Admin: sub menu doesnt display under apperance when activate my themes
- Display post category in foreach loop
- Single.php – Get Current Parent Category
- How to create a template for Pages?
- How do I stop the loop from repeating in my category template?
- get_the_category listing in hierarchial order
- How do I do a page break?
- Using wp_list_pages() after calling query_posts()
- Is it possible to add/tick a category to a post when it is created?
- Excluding posts not working
- Show posts count for Categories and Tags in wp_nav_menu
- Hide posts belongs to few categories in homepage
- How to add category post count in main navigation menu [closed]
- why does wordpress ignore the post args?
- post category in wp_insert_post
- Limit the number of posts a category can have – newest post goes in, oldest one drops out, possible? plugin?
- Block editors annoying warnings
- How to get subcategories from category slug?
- Remove current category from post but display all others
- How show categories in admin and get that selected to show posts in index
- disable Tab post on nav-menus page (Admin)
- Showing categories and subcategories with posts
- Change default category when I publish a post