Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been playing around with Wordpress' menu functionality (wp_nav_menu and wp_list_pages) to build a menu for my site. It's coming along, but I'm still having some trouble getting the behaviour I'd like.

What I'd like is something like the sidebar menu on this website (not WP): http://www.fairfood.org/facts/sustainability-agenda/

When you click on a subpage that has children, the menu displays those children: http://www.fairfood.org/facts/production-chains/

I currently have this code:

            <?php // sidebar menu
        if ($post->post_parent) {
            $ancestors=get_post_ancestors($post->ID);
            $root=count($ancestors)-1;
            $parent = $ancestors[$root];
        } else {
            $parent = $post->ID;
        }

        $children = wp_list_pages("title_li=&child_of=". $parent ."&echo=0");

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

This works, but this shows the children that all the subpages have, not just the children of the current page. An example of what I mean: http://test.fairfood.org/facts/

Any have any idea to make this work?

share|improve this question
    
As long as you call it late enough $wp_query should be able to give you the ID using get_queried_object_id(), so this should work.. $children = wp_list_pages("title_li=&child_of=" . $wp_query->get_queried_object_id() . "&echo=0"); –  t31os Feb 23 '11 at 17:04
    
even this could help wordpress.stackexchange.com/q/19507/6038 –  Matus Jun 10 '11 at 10:40

2 Answers 2

up vote 0 down vote accepted

If you dont mind using a plugin I can tell you how I have gotten this to work in the past.

  1. Add the following code to your functions.php
    function get_root_parent($page_id) {
    global $wpdb;
    $parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND ID = '$page_id'");
    if ($parent == 0) return $page_id;
    else return get_root_parent($parent);
    }
    
  2. Add the following to header.php between the close of head and open of body in a php wrapper
    <?php
    $GLOBALS["parentId"] = get_root_parent($post->ID);
    ?>
    
  3. Download and install the fold pages plugin: http://www.webspaceworks.com/resources/wordpress/30/

  4. Add the following code to your sidebar.php
    <ul>
    <?php
    $thispage = $wp_query->post;
    if ($thispage->post_parent!=0) {
            wswwpx_fold_page_list("sort_column=menu_order&child_of=".$GLOBALS["parentId"]."&title_li=&amp;child_of=".$thispage->post_parent);
          } else {
            wswwpx_fold_page_list("sort_column=menu_order&child_of=".$GLOBALS["parentId"]."&title_li=&amp;child_of=".$thispage->ID);
          }?>
        </ul>
    
share|improve this answer
    
This looks great! I'm going to check it out now. Thank you so much for your reply! –  user3201 Feb 23 '11 at 14:28
    
This worked perfectly! Thank you! –  user3201 Feb 25 '11 at 12:36

You can use this, to show submenu -> http://wordpress.org/extend/plugins/rv-submenus/

share|improve this answer

Your Answer

 
discard

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