1

I have the following problem:

There is a custom post type. There are several pages each listing some of the posts based on some post criteria (custom WP_Query).

When I click on a link to open the full version of the post, the parent page of the post is . But for the sake of user experience it should be the parent page the user came from and that should be highlighted in the menu.

I partly solved this problem by adding "?parent=" to the links and parsing that in a filter:

// Fix menu parents
add_filter('nav_menu_css_class', function($classes, $item, $args) {
    if (!isset($_GET['parent']) || empty($_GET['parent'])) return $classes;
    if ($item->object_id == $_GET['parent']) {
        $classes[] = "current_page_item";
        $classes[] = "current-menu-item";
    }
    return $classes;
}, 10, 3);

Now here comes the case, where this doesn't work: When the parent is inside a sub menu then the parent of that sub menu does not get marked as active and the sub menu remains closed. So I need to add current-page-ancestor current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor to the parent menu item inside that filter function.

So what I thought is, I need a $item->has_child($_GET['parent']) which does not exist obviously.

Do you guys have any ideas about this?

Edit: I added a dirty client side JavaScript fix to solve this. But I would still prefer a server side fix.

jQuery(function() {
    jQuery(".current-menu-item").parents("li").addClass("current-page-ancestor current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor");
});

1 Answer 1

0

It sounds like you may be over-complicating this. If your goal is to highlight a post's top parent while the post is listed in a sub-menu of said parent, then this is a CSS problem, not a wordpress one. The following should work:

.current-menu-parent a, .current-menu-ancestor a {
 color: #7dca8d;
}
4
  • That's not it. There is no current-menu-parent or current-menu-ancestor because the parent does not know it's active. Apr 12, 2018 at 11:26
  • There may be a problem with your code elsewhere in your custom theme, then. If a post has a parent set, then my solution should work regardless of how the user got to the post.
    – zoltar
    Apr 12, 2018 at 16:01
  • That is exactly my problem. The posts have two different parents dependant on where the user came from. So I need to set the parent manually. I don't see how to do that in a 'correct' way. Apr 13, 2018 at 10:26
  • If the parent is changing based on where they are coming from, then javascript is the way to go as that much dynamic altering would cause a lot of overhead on the database (especially if there is more than one user, but even one user could be a db crashing problem).
    – zoltar
    Apr 13, 2018 at 15:45

Your Answer

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

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