Hi, your support has been great – I contacted you earlier regarding this subject, and initially did not anticipate the need to hide non siblings of the current page 3 levels deep, but was wondering if that’s possible. You provided a function in this support request for up to 2 levels deep, and I tried this revised one below for 3 levels deep but it didn’t seem to work:
add_action( ‘advanced-sidebar-menu/menus/navigation/objects/parse-hierarchy’, function( array $hierarchy, $current, Objects $objects ) {
/* @var Item $current */
foreach ( $objects->menu_items as $id => $item ) {
if ( 0 !== (int) $item->menu_item_parent && ! $objects->is_in_current_family( $item->get_id() ) ) {
unset( $objects->menu_items[ $id ] );
}
}
if ( $current ) {
if ( $current->get_parent() ) {
//have to add a 3rd level here
if ( $current->get_parent()->get_parent()->get_children() ) {
// Three levels deep.
foreach ( $current->get_parent()->get_parent()->get_parent()->get_children() as $uncle ) {
if ( $current->get_parent()->get_parent() !== $uncle ) {
foreach ( $uncle->get_children() as $cousin ) {
unset( $objects->menu_items[ $cousin->get_id() ] );
}
}
}
}
elseif ( $current->get_parent()->get_parent() ) {
// Two levels deep.
foreach ( $current->get_parent()->get_parent()->get_children() as $uncle ) {
if ( $current->get_parent() !== $uncle ) {
foreach ( $uncle->get_children() as $cousin ) {
unset( $objects->menu_items[ $cousin->get_id() ] );
}
}
}
} else {
// One level deep.
foreach ( $current->get_parent()->get_children() as $uncle ) {
if ( $current !== $uncle ) {
foreach ( $uncle->get_children() as $cousin ) {
unset( $objects->menu_items[ $cousin->get_id() ] );
}
}
}
}
// On a top level item.
} else {
foreach ( $current->get_children() as $child ) {
foreach ( $child->get_children() as $grandchild ) {
unset( $objects->menu_items[ $grandchild->get_id() ] );
}
}
}
}
}, 10, 3 );
Also, alternatively, would you happen to have any examples of actions that would display an advanced sidebar menu on a page that is not actually inserted in the menu, but expanding its hierarchical parents? The function would have to identify the current page, then get it’s parents (not in the menu hierarchy, but in the page hierarchy), then expand the appropriate parents of that page, and then perhaps even insert that page into the displayed menu? I’m not even sure if something like this would be possible. I’m asking because we want certain sub pages to show up in the instance of our side expanded menu, but not in the main top menu.
OnPoint Plugins
Hi Deanne,
I have expanded the action from this support thread with the following addition:
As far as using the page hierarchy to adjust the display of the navigation menu:
I’m afraid navigation menus have their own hierarchy which is not aware nor linked to a page hierarchy. While theoretically possible to compute the intersection of the 2 hierarchies and make adjustments base on intersection, it would require a custom widget and is beyond what we can reasonably suggest via actions and filters.
Assuming you don’t want these missing widgets to show up in other menus, one solution would be to add these missing pages to a separate menu used specifically for the widget. I realize this means maintaining a separate menu but it might get the job done.
Have a great day!
Deanne Holzman
Thanks for your reply – your support has been above & beyond expectations and I appreciate that! What you’re saying about using page hierarchy to adjust the display of navigation (using a combination of navigation hierarchy and page hierarchy) totally makes sense.
With the current function that you suggested though, the menu items which are great grandchildren but not siblings of the current item are still displayed:
https://dalel5.sg-host.com/community/classes/persistent-pain-education/explain-pain-resources/
Here’s an illustration of what we would need:
https://imgur.com/a/UaV4WYc
https://imgur.com/a/NCrmioH
OnPoint Plugins
Hi Deanne,
I have adjusted the last to filter to also remove all great grandchildren from the menu unless they are the current menu item or a direct sibling of the current menu item.
Have a great day!
Deanne Holzman
Thanks alot for your help again – this is an excellent and very useful plugin – Overall it seems to work very well. Your function works perfectly on our main menu:
https://dalel5.sg-host.com/community/classes/mommy-baby-wellness/mommy-and-baby-wellness-program-registration/
hiding grandchildren when it should.
However when I use another ‘Advanced Sidebar navigation’ widget with a different menu (that also has great grandchildren), assuming the same function would change the way this menu is displayed, on any one of the grandchildren’s page, *all* the grandchildren show up (when I was hoping that only the current grandchild would):
https://dalel5.sg-host.com/foundation/ways-to-give/types-of-donations-funds/gift-in-memory/donate-in-memory-of-kelli-holland/
You can see that all the great grandchildren become visible on a ‘great grandchild’ page:
https://imgur.com/a/3hwWrjQ
And on it’s great grandparent page, the great grandchild shows up wierdly on the end of the menu:
https://imgur.com/a/ZQrUOiO
Would an ”advanced-sidebar-menu/menus/navigation/objects/parse-hierarchy’ action affect all menus displayed by the plugin?
You’ve gone above and beyond with my support requests so I’d understand if there’s not a solution for this, but any feedback you may have on why it would not behave the same way with a different menu would be greatly appreciated!
OnPoint Plugins
Hi Deanne,
When on a great grandchild page, the action as written will remove other great grandchildren only. As the items mentioned in this screenshot are only first level grandchildren, they are not removed.
https://imgur.com/a/3hwWrjQ
I could adjust the action to also remove grandchildren from the menu, but the action does affect all menus so it will also remove the grandchildren from the original menu.
Deanne Holzman
Thanks for your reply!
Yes, here are a few images to show what I mean – with the last filter you wrote that removes all great grandchildren from the menu unless they are the current menu item or direct sibling of the current menu item, we are getting other ‘grandchildren’ showing up in other sections – if you could adjust the action to also remove the grandchildren from the original menu (unless of course that particular grandchild is the current page) that would be great.
https://imgur.com/a/arA0dak
Also, when viewing pages within some great grandchildren’s section pages (but not the great grandchildren’s pages themselves) we have an odd instance of ‘great grandchildren’ items being appended to the main menu as top level menu items – is there any way to avoid this:
https://imgur.com/a/pnX3mfW
https://imgur.com/a/1hiNjSZ
the menus that I’m referring to:
https://dalel5.sg-host.com/community/
https://dalel5.sg-host.com/foundation/ways-to-give/
OnPoint Plugins
Hi Deanne,
I have made the following adjustments to the action:
Have a great night!
Deanne Holzman
Thanks alot, this is exactly what we needed. I really appreciate your great customer service and useful plugin,