Skip to content

Instantly share code, notes, and snippets.

@carasmo
Last active December 11, 2015 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carasmo/eaa52fd3cfbf75358efc to your computer and use it in GitHub Desktop.
Save carasmo/eaa52fd3cfbf75358efc to your computer and use it in GitHub Desktop.
<?php
//don't add the opening php
// goes inside functions.php file in a child GENESIS CHILD THEME ONLY
/*====== PAGE CHILDREN ===================================================================================================================================================
/* If the page has sub pages, those pages and the title of the parent page shows up as a menu in the sidebar (if there's a sidebar)
/* if the page is set to full width, then nope
/* @genesis_sidebar
/* ul child is .children
/* reference in part List topmost ancestor and its immediate children title on this page:
https://codex.wordpress.org/Function_Reference/wp_list_pages#List_subpages_even_if_on_a_subpage
/* @since 1.0.0
=========================================================================================================================================================================== */
add_action( 'genesis_sidebar', 'cab_child_pages', 5 );
function cab_child_pages() {
if (is_page()) {
global $post;
if(!function_exists('get_post_top_ancestor_id')){
function get_post_top_ancestor_id(){
global $post;
if($post->post_parent){
$ancestors = array_reverse(get_post_ancestors($post->ID));
return $ancestors[0];
}
return $post->ID;
}
} // end if the function does not exist create one
// check if the the page has children
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");
//show on pages with children
if ($children) {
echo '<div class="widget widget_nav_menu"><ul>';
wp_list_pages( array('title_li'=>'','include'=>get_post_top_ancestor_id()) );
wp_list_pages( array('title_li'=>'','depth'=>4,'child_of'=>get_post_top_ancestor_id()) );
echo '</ul></div>';
}
} //end if is page
} //end cab_child_pages function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment