WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Trying to show a wp menu based on parent. Works great on parent / child pages, but on grandchildren pages its failing. How should i restructure the menu selector to include the sub nav menu based on the main parent?

if ( wp_get_nav_menu_object( $post->post_title ) ) {
wp_nav_menu( array( 
'menu' => $post->post_title,
'menu_class' => 'subnav',
'container' => ''
) );
} else {

   if ( wp_get_nav_menu_object( get_the_title( $post->post_parent ) ) ) {
wp_nav_menu( array( 
'menu' => get_the_title( $post->post_parent ),
'menu_class' => 'subnav',
'container' => ''
) );


share|improve this question
Just a note, this DOES work to show the immediate child, but is failing when i go to a grandchild. – Matt Adams Mar 20 '12 at 19:22
up vote 1 down vote accepted

Work around.

if (is_page( 'pagename' ) || in_array("5", $post->ancestors)) {
wp_nav_menu( array( 'container_class' => 'subnav', 'menu' => 'visit' ));


5 being the ID of the page in question.

So while this is a bit more work, needing and if / if:else statement for each page, it will scale to allow as many child / grandchild pages as needed

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.