1

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!

1
  • 2
    Post all of the code please. I assume this is part of a larger function hooked into a filter? Which one?
    – s_ha_dum
    Commented Jul 20, 2015 at 2:44

1 Answer 1

0

It seems to me, you are trying to get the ancestors by the actual post, while you most likely should get the ancestors according to the menu item. Why? Because setting up a hierarchy in the menu does not change the actual post, especially the post_parent property won't be changed - in short: (other) post type and menu item (which actually is a post type too) related changes are not mutually mimicked.

And, after all you can have hierarchical as well as non-hierarchical post types in one menu, not to mention, all the other types of menu items that can be set. Non-hierarchical post types for example, while they have the post_parent column in the database, there actually isn't a built-in way to set it, wouldn't make sense anyway as they are not supposed to have a hierarchy. Not even going into the other kinds here, but to say, many of them not actually even capable - or at least are not intended to have one as they are built - of having a hierarchy - with the exception of being part of a menu, but than - as said - the information is part of the menu, not the item put into the menu.

Last but not least, even if you are working with a hierarchical post type you probably want to get the ancestors by the menu item, because otherwise you would have to mimic the hierarchy between the post type and the menu, meaning you have to do every hierarchy change twice. When your menu solely depends on a hierarchical post type, then you probably should go for a solution that takes that in account and generates your menu according to the hierarchy automatically - to spare you the task of manually doing it, while you could automate it.

2
  • The idea is that the navigation menu only has a few items on it, and all the other pages are (variously deep) children of this one page. Therefore, the menu being crawled is nowhere near all-inclusive. Perhaps I'm misunderstanding the way the menu item behaves, but isn't it just limited to the (say, 6) items in the menu? Rather than the (say, 100) pages scattered about as various levels of children? Most importantly, I have a Documentation nav item, and that has many children, ~ 3 deep. My idea was, on a given page, check that page and see if its topmost parent was Documentation Commented Jul 20, 2015 at 19:27
  • I can't explain to you further how the nav menu system of WP works, but that is well documented, so you should not have trouble getting the information you want. But certainly you have some misconception going on, I think you are probably more looking for something along the lines of wp_list_pages(). @PhilipKahn Commented Jul 20, 2015 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.