I have a menu on a page that shows the children of a parent page, however, if a sub page is clicked on the children only of the sub page shows in this nav. is it possible to haver the direct children of the master parent page?
I’m currently using the following:
$thispage = $wp_query->post;
if($thispage->post_parent!=0){
wp_list_pages("title_li=&child_of=".$thispage->post_parent);
}else{
wp_list_pages("title_li=&child_of=".$thispage->ID);
}
This is what I have used in a few projects that always displays the children pages even if you’re are viewing a child page. See if this is what you had in mind
Your current logic is:
And your issue is what happens when you arrive on a page that’s 3 levels deep? We can check if we have a parent or not, but we also need to check if that parent has a parent too, aka a grandparent.
So this would do the trick:
But clearly this doesn’t scale! What about 4 levels deep? 5 levels deep? What if my page has a parent, which has a parent, which has another parent? Too many levels! So confusing, and so many nested if else statements.
So how do we do this correctly?
Recursion
For a truly robust answer, we need to use a recursive function. We don’t know how many levels deep we are, so we can’t account for every single level. Not without having huge if else statements and nightmarish code.
So instead, lets do something similar to this:
Where “show posts” is:
In more ‘PHP-like’ code: note, this is pseudocode, not actual code
Now we have something that will work for 1 level deep, 2 levels deep, 3 levels deep ….. 5000 levels deep, 5001 levels deep, etc etc etc