views:

874

answers:

2

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.

A: 

Try using get_post_ancestors. This approach seemed to work for me in a similar situation:

<?php
global $wp_query;
$post = $wp_query->post;
$ancestors = get_post_ancestors($post);
if( empty($post->post_parent) ) {
    $parent = $post->ID;
} else {
    $parent = end($ancestors);
} 
if(wp_list_pages("title_li=&child_of=$parent&echo=0" )) { ?>

<ul id="secondary-nav">
    <?php wp_list_pages("title_li=&child_of=$parent&depth=1" ); ?>
</ul><!-- #secondary-nav -->

<?php } ?>

Then I used this to target the current nav states with CSS:

#secondary-nav li a:hover,
#secondary-nav li.current_page_item a,
#secondary-nav li.current_page_ancestor a {
    background:#fcb701;
}

You'll probably need to remove the depth parameters to show you're 3rd level pages.

I hope this helps!

Kevin
A: 

I have a very similar problem, except that the client would also like to include text excerpts from the child pages in the menu. I can't get excerpts from wp_list_pages so I wonder if anyone can suggest how to adapt Kevin's solution to use a wp_query or similar?

Any suggestions much appreciated.

papergecko