How to add class to current and parent menu items if menu contains pages and categories that have the same ID?

153 Views Asked by At

I have created a wordpress menu that has pages and taxonomy archive (categories). The structure is like:

Sample Page (Page with ID: 24)
    -> Sample Category (Category/Taxonomy with ID: 25)
    -> Another Category (Category/Taxonomy with ID: 26)
Another Page (Page with ID: 25)

Now I am trying to add the class 'current-item' and 'current-ancestor' to their respective menu items.

The issue I am having is that if I select Sample Category (with ID:25) WP is adding the class to it but also to Another Page (with ID: 25).

I have built a mega menu that checks the current menu item with the get_queried_object so that is where my issue lies. Since the queried object has an id of 25, how can I distinguish between the two different menu items?

$active = ( $menu_item->object_id == get_queried_object_id() ) ? 'current-menu-item' : '';
$postancestors = get_post_ancestors( get_queried_object_id() );
$activeparent = ( in_array( $menu_item->object_id, $postancestors ) ) ? 'current-menu-ancestor ' : ''
0

There are 0 best solutions below