/**
* Template tag to display subnavi, which is a sidebar navigation.
*
* It is to be used in hierarchical Page
* Currently, it can only work when there are only one lv.3 exists.
*
* Usage example: echo tujstarter_subnavi( '14' );
*
* @since TUJ Starter 1.1
* @todo To make it work when there are more than one lv.3 Pages. Smarter way to specify lv.3 page id.
*/
if ( ! function_exists( 'tujstarter_subnavi' ) ) :
function tujstarter_subnavi( $lv3root = 0 ) {
// Leave a message and exit, if it is called when not in Page.
if ( !is_page() ) {
echo "<b>tujstarter_subnavi() is only usable when is_page() is true</b><br /><br />";
return;
} else {
global $post;
$subnavi_output ='';
$root_url = '';
$root_title = '';
$args = array();
// sanitize, mostly to keep spaces out
$lv3root_id = preg_replace('/[^0-9,]/', '', $lv3root);
// if this post/page has a parent (= lv.2 or 3 page), do this
if( $post->post_parent ) {
// get the parent's ID of this post/page.
$subnavi_get_parent_id = get_post( $post->post_parent );
// then use this parent's ID to get its parent's ID and call this root
$subnavi_root_page_id = $subnavi_get_parent_id->post_parent;
// if root page id is zero it means lv.2, so get its parent's details
if ( $subnavi_root_page_id == 0 ) {
$root_url = get_permalink( $post->post_parent );
$root_title = get_the_title( $post->post_parent );
if ( $lv3root_id == $post->ID ) {
// if this is a lv.3 Page specified in parameter, display all depth
$args = array( 'title_li' => '', 'child_of' => $post->post_parent, 'echo' => 0, );
} else {
// if it is not, then display only 1 depth
$args = array( 'depth' => 1, 'title_li' => '', 'child_of' => $post->post_parent, 'echo' => 0, );
}
} else {
// otherwise, it means lv.3, so get its parent's parent's deitals
$root_url = get_permalink( $subnavi_root_page_id );
$root_title = get_the_title( $subnavi_root_page_id );
$args = array( 'title_li' => '', 'child_of' => $subnavi_root_page_id, 'echo' => 0, );
}
} else {
// otherwise, it means this post/page has no parent (=lv.1), so just get its own details
$root_url = get_permalink( $post->ID );
$root_title = get_the_title( $post->ID );
$args = array( 'depth' => 1, 'title_li' => '', 'child_of' => $post->ID, 'echo' => 0, );
}
// lets construct and output if has any child pages to list
$output_page_list = wp_list_pages( $args );
if ( $output_page_list ) {
// construct the title and list output in coding style of TUJ Core v4.
$subnavi_output = '<nav class="secnavi-vert bm-30px">';
$subnavi_output .= "\n\t<div>\n\t";
$subnavi_output .= '<h1><a href="' . $root_url . '">' . $root_title . '</a></h1>';
$subnavi_output .= "\n\t\t<ul>\n\t\t\t" . wp_list_pages( $args ) . "\n\t\t</ul>\t";
$subnavi_output .= "</div>\n</nav>";
// at last, return the output!
return $subnavi_output;
} else {
return;
} // END if ( $output_page_list )
} // END if( $post->post_parent )
}
endif;