Skip to content

Instantly share code, notes, and snippets.

@brendannee
Created September 27, 2010 05:55
Show Gist options
  • Save brendannee/598672 to your computer and use it in GitHub Desktop.
Save brendannee/598672 to your computer and use it in GitHub Desktop.
List top level pages in Wordpress
<?php
$parents = get_post_ancestors($post);
if(count($parents)>=2):
//Page is a grandchild, show highest level parents as menu
echo '<ul>';
wp_list_pages('title_li=&child_of='.end($parents));
echo '</ul>';
elseif(count($parents)==1):
//Page is a child
echo '<ul>';
wp_list_pages('title_li=&child_of='.$parents[0]);
echo '</ul>';
elseif(wp_list_pages("child_of=".$post->ID."&echo=0")): // page is top level and has children
echo '<ul>';
wp_list_pages('title_li=&child_of='.$post->ID);
echo '</ul>';
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment