Skip to content

Instantly share code, notes, and snippets.

@maljones
Created November 10, 2016 18:58
Show Gist options
  • Save maljones/05f1be399ce146147efe8c52fe1c4deb to your computer and use it in GitHub Desktop.
Save maljones/05f1be399ce146147efe8c52fe1c4deb to your computer and use it in GitHub Desktop.
WordPress Parent/Child Nav
/**
* Generate Parent/Child Page Menus
* @link https://codex.wordpress.org/Function_Reference/wp_list_pages
*/
function print_menu_child_pages(){
$children = '<nav class="nav-children nav-section"><ul id="menu-section">';
global $post;
if($post->post_parent){
// Post Has Parent!
// Sets Variables for determining what type of page the nav is on
$parentID = $post->post_parent;
$parentPost = get_post($parentID);
$parentTemplate = get_page_template_slug( $parentID );
$grandparentID = $parentPost->post_parent;
$grandparentPost = get_post($grandparentID);
if($grandparentID){
// If Has Grandparent
$children .= wp_list_pages("title_li=&include=".$grandparentID."&echo=0");
$children .= wp_list_pages("title_li=&child_of=".$grandparentID."&echo=0");
} else {
$children .= wp_list_pages("title_li=&include=".$post->post_parent."&echo=0");
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
}
} else {
// Post Has No Parent!
$children .= wp_list_pages("title_li=&include=".$post->ID."&echo=0");
$children .= wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}
// Print Subnav
if($children){
echo $children;
}
echo '</ul></nav>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment