I have seen very similar (if not exact) queries to this all over Google, but always seem to find something short of what I need. I am trying to create my full vertical menu using wp_nav_menu in my template. Only I would like to only show the current branch, siblings, and their children. Kind of an accordion-style menu. I prefer to do this in PHP, not using CSS or JQuery.
Example full menu:
- Home
- Products
- Cool Item 1
- Item 1 Details
- Item 1 Gallery
- Cool Item 2
- Item 2 Details
- Item 2 Gallery
- Services
- Svc 1
- Svc 2
If, for example, I am on current item "Cool Item 1", I want to see all Root menu items, all Products, but only children of "Cool Item 1", no children of "Cool Item 2" or any Services. Like below:
- Home
- Products
- Cool Item 1 (-current page-)
- Item 1 Details
- Item 1 Gallery
- Cool Item 2
- Services
If I click on Services, I want to see:
- Home
- Products
- Services (-current page-)
- Svc 1
- Svc 2
Currently in my custom walker I have the star_el method like so: (make_menu_item is typical menu display you'd see searching walkers)
function start_el(&$output, $item, $depth, $args) {
// am I on the current Item?
if($item->current_item_ancestor or $item->current_item_parent or $item->current){
$cur_branch = true;
}
// this works for all current branch items, but not siblings or children
if( $cur_branch || $depth == 0 ) {
$output .= $this->make_menu_item($item, $depth, $args);
} else {
// need to know if my parents are in current branch, then OK to show children
// not sure how to do that from this point?
if($item->menu_item_parent && $depth > 0) {
// this currently shows everything, not what I want
// if this is removed, no children objects show, even under cur_branch
$output .= $this->make_menu_item($item, $depth, $args);
}
}
}
I know this logic probably isn't the best, but its a start. I can easily tell if I am in the "current branch" of the menu, and I have setup $item->hasChildren in the display_elements method, but hasChildren doesn't do me any good for children of current. It would be most helpful I just knew at this point whether or not my parent object is in the current branch, because I could either show the menu item or not then, which is what I want.
I could do an SQL query, but I sure rather not do that. Is there a way to get parent info in the walker array's and objects available ($item or $elements?) ? Maybe I am going about this all wrong? Seems like it should be easy to get parent info, but calling get_ancestors() here is giving me an empty array.
Thank you for any help! Greg