I’ve been messing around / searching for hours and still can’t get this to work, so i’m finally giving in and asking for some help.
I’m trying to write a custom walker that shows only the current pages children, or if there are no children display the pages siblings.
For example, take the following menu tree:
- 1.0
- 1.2.0
- 1.3.0
- 1.3.1
- 1.3.2
- 1.2.1
- 1.2.2
- 1.2.0
- 2.0
Let’s assume that i am on the current page 1.2.0. On this page I want to display it’s children (1.3.0, 1.3.1, 1.3.2)
however, if i’m on the page 1.2.2, since it doesn’t have any children, it should display it’s current level siblings, so it should show me (1.2.0, 1.2.1, 1.2.2).
This is the walker I used to display only children of the current menu item. Or the menu items siblings if it doesn’t have any children of its own.
There are comments throughout the class explaining each section
Attach it like you would with any other custom walker on a wp_nav_menu
I had a similar experience. You may want to think about moving the pages logic out of the walker. Basically, compile the current page hierarchy as an object. Then use the ‘exclude’ parameter in the wp_nav_menu function. Now the excluded pages would depend on if the current page has children. If no children show brothers; if children && those children are the end of the line show brothers and children; if children && and grandchildren exist exclude brothers and show children and grandchildren.