Skip to content

Instantly share code, notes, and snippets.

@brettsnippets
Last active December 17, 2015 13:19
Show Gist options
  • Save brettsnippets/5615798 to your computer and use it in GitHub Desktop.
Save brettsnippets/5615798 to your computer and use it in GitHub Desktop.
WordPress: is_tree
//Page List Navigation
function my_page_tree($this_page) {
$pagelist = '';
if( !$this_page->post_parent ) {
$children = wp_list_pages('sort_column=menu_order&exclude=430&title_li=&child_of='.$this_page->ID.'&echo=0');
if( $children ) {
$pagelist .= '<li class="current_page_item"><a href="'. get_page_link($this_page->ID) .'">' . $this_page->post_title . '</a>';
$pagelist .= '<ul>' . $children . '</ul>';
$pagelist .= '</li>';
}
}
elseif( $this_page->ancestors ) {
// get the top ID of this page. Page ids DESC so top level ID is the last one
$ancestor = end($this_page->ancestors);
$pagelist .= wp_list_pages('order=asc&sort_column=menu_order&exclude=430&title_li=&include='.$ancestor.'&echo=0');
$pagelist = str_replace('</li>', '', $pagelist);
$pagelist .= '<ul>' . wp_list_pages('order=asc&sort_column=menu_order&&exclude=430&title_li=&child_of='.$ancestor.'&echo=0') .'</ul></li>';
}
return $pagelist;
}
//Tree function
function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
if(is_page()&&($post->post_parent==$pid||is_page($pid)))
return true; // we're at the page or at a sub page
else
return false; // we're elsewhere
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment