I hope this isn't a dup -- I wasn't able to find it!
My goal is to, in my custom walker, append the current-menu-ancestor
class to a menu item if it's an ancestor (no matter how deep) of the page/post the user is currently on.
I was hoping something like this would do the trick: (in my wp_menu_item_classes_by_context()
equivalent)
if ($post->post_parent) { // Does the post have parents at all?
$ancestors=get_post_ancestors($post->ID); // Get all ancestors
$root=count($ancestors)-1; // Get the key of the top-level one
$parent = $ancestors[$root]; // Top level ancestor ID
// Finally, see if the ancestor ID is the same as the menu_item's post ID
if($parent == $menu_item->ID) $classes[] = "current-menu-ancestor";
}
To no avail.
I'm SURE it's not much more complex than this, but I'm not particularly well versed in Wordpress idiosyncratics!