I’m working on a website at the moment, and the navigation works as follows (by client specification).
Underneath the header there is a horizontal navigation listing the top level pages, clicking on one of these takes you to page.php, which has a vertical navigation in the sidebar listing the sub-pages of that particular page like this:
2nd level
– 3rd level
– 3rd level
– 3rd level
2nd level
– 3rd level
– 3rd level
– 3rd level
and so on, and so on.
This is the code I am currently using in the vertical navigation:
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children)
{
<ul>
echo $children;
</ul>
}
What I would like to be able to do is continue to have the same vertical navigation, regardless of the current page level. I’m finding it hard to list the sub-pages of a 1st level page when you’re on a 3rd level page.
Any suggestions greatly appreciated.
Try using
get_post_ancestors
. This approach seemed to work for me in a similar situation:Then I used this to target the current nav states with CSS:
You’ll probably need to remove the depth parameters to show you’re 3rd level pages.
I hope this helps!
I think this can be of your help. I had same issue. I had to show up parent page’s layout stuffs (everything) in child page.
In my case I had to load blurbs of parents. I used WordPress Registry plugin to store $parents[1] id. Then I fetched the blurbs of parents by simply passing parent page id in the function where blurbs data are fetched.
Once you’re inside
The Loop
, it’s very easy to pull up a reverse history of all page ancestors.The key line of course is
$history = array_reverse( array_map( 'get_post', get_post_ancestors( $post ) ) );
This does two things:get_post_ancestors()
to actualWP_Post
objects (which, isn’t strictly necessary, since all we really need are IDs to pass toget_permalink()
andget_the_title()
)get_post_ancestors()
puts the immediate parent at the top of the list, and we probably want it at the bottom.