Skip to content

Instantly share code, notes, and snippets.

@johnheimkes
Created May 27, 2015 22:18
Show Gist options
  • Save johnheimkes/5aa923e56eb6eab58221 to your computer and use it in GitHub Desktop.
Save johnheimkes/5aa923e56eb6eab58221 to your computer and use it in GitHub Desktop.
WordPress child page submenu & ancestors
<?php
$ancestors = get_post_ancestors($post->ID);
if (empty($ancestors)) {
$last_ancestor = $post->ID;
} else {
$last_ancestor = $ancestors[count($ancestors) - 1];
}
?>
<ul class="sidebar-menu v-list text-center">
<?php
$nav_items = get_pages(array(
'parent' => $last_ancestor,
'post_status' => 'publish',
'sort_column' => 'menu_order'
));
foreach ($nav_items as $nav)
{
if ($post->ID == $nav->ID)
{
echo '<li class="current_page_item"><a href="' . get_permalink($nav->ID) . '">' . $nav->post_title . '</a>';
}
else
{
echo '<li><a href="' . get_permalink($nav->ID) . '">' . $nav->post_title . '</a>';
}
if ($post->ID == $nav->ID || (count($ancestors) > 1 && $post->post_parent == $ancestors[0]))
{
$child_nav_items = get_pages(array(
'parent' => $nav->ID,
'post_status' => 'publish',
'sort_column' => 'menu_order'
));
if ( count($child_nav_items) > 0) {
echo '<ul class="sidebar-sub-menu v-list">';
foreach($child_nav_items as $child_nav)
{
if ($post->ID == $child_nav->ID)
{
echo '<li class="current_page_item"><a href="' . get_permalink($child_nav->ID) . '">' . $child_nav->post_title . '</a></li>';
}
else
{
echo '<li><a href="' . get_permalink($child_nav->ID) . '">' . $child_nav->post_title . '</a></li>';
}
}
echo '</ul>';
}
}
echo '</li>';
}
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment