Submenu based on WordPress pages with added hierarchy. Will show submenu of current page, and if necessary sub-submenu
<?php
// Generate correct submenu
function show_subnav() {
global $post;
$ancestors = get_post_ancestors($post);
if( empty($post->post_parent) ) {
$parent = $post->ID;
} else {
$parent = end($ancestors);
}
if( isset( $parent ) ):
foreach( get_pages( array(
"parent" => $parent,
) ) as $page ):
echo '<li class="page ' . ( $post->ID == $page->ID ? "current-page":"") . '"><a href="' . get_permalink( $page->ID ) . '">' . $page->post_title . '</a>';
$children = get_pages( array( 'parent' => $page->ID ) );
if( $children && ( $post->ID == $page->ID || in_array( $page->ID, $ancestors ) ) ){
echo '<ul class="children">';
foreach( $children as $child ){
echo '<li class="page sub-page ' . ( $post->ID == $child->ID ? "current-page":"") . '"><a href="' . get_permalink( $child->ID ) . '">' . $child->post_title . '</a>';
$subchildren= get_pages( array( 'parent' => $child->ID ) );
if( $subchildren && ( $post->ID == $child->ID || in_array( $child->ID, $ancestors ) ) ){
echo '<ul class="sub-children">';
foreach( $subchildren as $subchild ){
echo '<li class="page sub-child ' . ( $post->ID == $subchild->ID ? "current-page":"") . '"><a href="' . get_permalink( $subchild->ID ) . '">' . $subchild->post_title . '</a>';
$subsubchildren= get_pages( array( 'parent' => $subchild->ID ) );
if( $subsubchildren && ( $post->ID == $subchild->ID || in_array( $subchild->ID, $ancestors ) ) ) {
echo '<ul class="subsub-children">';
foreach( $subsubchildren as $subsubchild ) {
echo '<li class="page subsub-child ' . ( $post->ID == $subsubchild->ID ? "current-page" : "" ) . '"><a href="' . get_permalink( $subsubchild->ID ) . '">' . $subsubchild->post_title . '</a></li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
}
echo '</li>';
endforeach;
endif;
}