Highlight current post ancestor parent menu items


The default menu classes in WordPress are pretty useful. But one problem that i stumble over from time to time is with categories as subitems in menus. For example the following menu structure:

  • Page 1
  • Page 2
    • Category 1
  • Page 3

So when Category 1 is active Page 2 gets .current-menu-ancestor which is fine … but as soon as a post of Category 1 is viewed Page 2 has no specific classes … But Category 1 has .current-menu-ancestor as expected.

So finally here’s the question: How do i assign a class for these .current-post-ancestor parents?

I’m searching for a PHP solution. Javascript/jQuery is pretty clear … here is a jQuery solution for better understanding what i want to do (And for those who have the same problem and are happy with a JS solution):

jQuery( 'li.current-post-ancestor' ).parents( 'li.menu-item' ).addClass( 'current-menu-ancestor' );
, GDY 5 years 2018-07-04T01:55:26-05:00 0 Answers 93 views 0

Leave an answer