WordPress, printing ParentChild menus

Worpress allows you to print parent menu’s with different child-menus. Implementation is easy, just add this to your themes.

In the file that creates the header of your page, normally header.php, add this code:

     <?php   $args = array(
            'title_li' => '',
            'sort_column'=>'menu_order',
            'depth' => 1
                  );
    wp_list_pages($args);
    ?>

Then in the file that creates your child menu (normally this should be sidebar.php) add this code:

   <?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 } ?>

The selected menus are li.current_page_parent and li.current_page_item so you should adjust their style in your stylesheet.

Leave a Reply

Your email address will not be published. Required fields are marked *