10

Previously, I was able to selectively load child pages for a currently-selected parent page using logic such as:

if(  $post->post_parent ) {
  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
} else {
  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}

if ($children) { ?>
   <ul id="subnav">
     <?php echo $children; ?>
   </ul>
<?php 
} else {
}

There doesn't seem to be a native way to do this using the new register_nav_menus()/wp_nav_menu() functionality. Anyone know how I could patch this together at this point?

Here's a screenshot of what I'm trying to achieve:

Drop down Child menu screenshot

12
  • This is tough, since wp_nav_menu doesn't relate to page hierarchy (or pages at all, for that matter). I don't know enough about menus to answer this now, but this is a very interesting question. Commented Aug 18, 2010 at 21:31
  • I don't fully understand the question. What are you trying to accomplish? I see the code but I don't get the context. Got a site that does want? A screenshot? Commented Aug 18, 2010 at 21:39
  • He wants to add child items of top level menu items conditionally (i.e. only if that tree structure is active) in the new nav menu api. Commented Aug 18, 2010 at 21:40
  • @John P. Bloch - What does "only if the tree structure is active" mean? Are we talking Pages only? What about Posts, Categories, Tags, Custom Posts, etc? Does he want to do it on the client via jQuery or in the server? I guess I'd rather not assume and I'd like to hear more about the actual use-case. Commented Aug 18, 2010 at 21:45
  • @MikeSchinkel In this particular case, I think we may be talking pages only, but that doesn't matter anyway; the solution will still be the same. I think ZaMoose wants it done server-side. Commented Aug 18, 2010 at 21:55

4 Answers 4

9

I created a Widget named Page Sub Navigation (clever I know) that is working for me.

If you install this, you can just drag the widget to one of your widget areas and BAM it works.

<?php
/*
Plugin Name: Page Sub Navigation
Plugin URI: http://codegavin.com/wordpress/sub-nav
Description: Displays a list of child pages for the current page
Author: Jesse Gavin
Version: 1
Author URI: http://codegavin.com
*/

function createPageSubMenu()
{
  if (is_page()) {
    global $wp_query;

    if( empty($wp_query->post->post_parent) ) {
      $parent = $wp_query->post->ID;
    } else {
      $parent = $wp_query->post->post_parent;
    }

    $title = get_the_title($parent);

    if(wp_list_pages("title_li=&child_of=$parent&echo=0" )) {
      echo "<div id='submenu'>";
      echo "<h3><span>$title</span></h3>";
      echo "<ul>";
      wp_list_pages("title_li=&child_of=$parent&echo=1" );
      echo "</ul>";
      echo "</div>";
    }
  }
}


function widget_pageSubNav($args) {
  extract($args);
  echo $before_widget;
  createPageSubMenu();
  echo $after_widget;
}

function pageSubMenu_init()
{
  wp_register_sidebar_widget("cg-sidebar-widget", __('Page Sub Navigation'), 'widget_pageSubNav');
}
add_action("plugins_loaded", "pageSubMenu_init");
?>

Or if you just want the juicy parts...

if (is_page()) {
  global $wp_query;

  if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
  } else {
    $parent = $wp_query->post->post_parent;
  }

  if(wp_list_pages("title_li=&child_of=$parent&echo=0" )) {
    wp_list_pages("title_li=&child_of=$parent&echo=1" );
  }
}

UPDATE

I found another plugin that does essentially the same thing (and maybe does it better, I don't know). http://wordpress.org/extend/plugins/subpages-widget/

1
  • This is SO, so close to what I need. The only problem is that it misbehaves when BuddyPress is active.
    – ZaMoose
    Commented Aug 19, 2010 at 14:38
2

you could do a css hack to do this (2 ways that I would try)

1 this is the easiest way I can think of do make the css display the items in the subnavigation.

.current-menu-ancestor ul {display:inline;}
.current-menu-parent ul (display:inline;}

2 assuming that your theme supports body classes you could create a nav menu for each "sub nav", and set them to display beneath the main navigation - then edit your stylesheet to only show the subnav div's using something like this:

.child-menu-about, .child-menu-leadership {display:none;}
body.page-id-YOUR_ABOUT_PAGE_ID .child-menu-about {display:inline;}
body.category-YOUR-CATEGORY-SLUG  .child-menu-leadership {display:inline;}
0

enter image description here 1 this is the php display .

enter image description here 2 this is the css display .

1
  • Posting answers as code screenshots is the worst possible way - please reedit your answer. A few words of explanation wouldn't hurt also.
    – Picard
    Commented Jul 11, 2017 at 11:15
0
<nav class="site-nav children-link">
                <?php       

                    if(  $post->post_parent ) 
                    {
                      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
                    } 
                    else 
                    {
                      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
                    }

                    if ($children) { ?>
                       <ul>

                            <?php echo $children; ?>

                       </ul>

                    <?php 
                        } else {
                        }
                ?>
        </nav>

CSS

/*children-links links*/

.children-link 
{       

        background-color: #1a5957;
        color:#FFF;
        font-size: 100%;

}

.children-link li
{
    margin: 10px;   


}

.children-link ul li a:link,
.children-link ul li a:visited 
{
        padding: 15px 17px;
        text-decoration: none;
        border: 1px solid #1a5957;

}
.children-link ul li a:hover 
{
        background-color: #1a5957;
        color:#FFF;
        font-weight: bold;

}
.children-link .current_page_item a:link,
.children-link .current_page_item a:visited
{

    background-color: #1a5957;
    color: #FFF;
    cursor: default;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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