Wordpress custom menus are a very valuable feature, as they allow creating hierarchichal menus in the backend. Sometimes it would be useful to just use portions of this menu, e.g when displaying a submenu on a page, containing links to the children of the currently selected parent page in the main menu. Unfortunately Wordpress does not yet provide a simple option for displaying just the children of a particular menu item, so a little more effort is needed to accomplish this task.

Figure out the parent page’s ID

 $post_root = 0;
 $p = $post;

 while( $p->post_parent != 0){

   $p = get_post( $p->post_parent );
   $post_root = $p;
 }

 if($post_root == 0)
  $post_root = $post;

Get a list of the post root’s children

 $children = get_pages( array('child_of' => $post_root->ID ) );

Define a custom Walker which will be used to iterate the menu

 class ChildMenuWalker extends Walker_Nav_Menu{

  protected $child_ids = array();
  protected $exclude = -1;

  function __construct($children, $exclude){

   $this->child_ids = $children;
   $this->exclude = $exclude;
  }

  function start_el(&$output, $item, $depth, $args){

    if( ! in_array($item->object_id, $this->child_ids) ){
     return;
    } elseif( $item->object_id != $this->exclude) {
     parent::start_el(&$output, $item, $depth, $args);
    }

   }
 }

the “start_el” function checks wheter the current item is in the children list. If so, the default Walker’s functionality is used, otherwise nothing is done (output remains empty). Pay attention to use $item->object_id, as this is the post object’s id.

Display the menu with wp_nav_menu using the custom walker class.

 wp_nav_menu( array(

  'menu' => 'main-menu',
  'walker' => new ChildMenuWalker($children_ids, $post_root->ID )
 ) )

This renders a menu, containing only the child elements of the selected post root.