Skip to content

Instantly share code, notes, and snippets.

@MikeNGarrett
Last active April 22, 2019 10:00
Show Gist options
  • Save MikeNGarrett/5686183 to your computer and use it in GitHub Desktop.
Save MikeNGarrett/5686183 to your computer and use it in GitHub Desktop.
Instead of wp_list_pages where all children and grandchildren of the current page are listed out this method lists the parent page, siblings of the current page (in menu order) and when it gets to the current page it lists out the children.
<?php /* Not as simple as it sounds */ ?>
<div class="side-nav">
<div class="holder">
<ul>
<?php
if (isset($post->post_parent) && $post->post_parent > 0) {
$permalink = get_permalink($post->post_parent);
$parent_title = get_the_title($post->post_parent);
print('<li class="page_item page-parent"><a href="'.$permalink.'">'.$parent_title.'</a></li>');
$parent = $post->post_parent;
} else {
$parent = $post->ID;
}
$page_array = array();
$pagers = get_pages(array('sort_column' => 'menu_order', 'hierarchical'=> 0, 'child_of' => $post->post_parent, 'parent' => $post->post_parent));
foreach ($pagers as $pager) {
$permalink = get_permalink($pager->ID);
$class = "page_item page-item-".$pager->ID;
if ($post->ID == $pager->ID) {
$class .= " current_page_item";
}
print('<li class="'.$class.'"><a href="'.$permalink.'">'.$pager->post_title.'</a>');
if ($post->ID == $pager->ID) {
print('<ul>');
$children = get_pages(array('sort_column' => 'menu_order', 'hierarchical'=> FALSE, 'child_of' => $pager->ID, 'parent' => $pager->ID));
//$children = get_page_children( $pager->ID, $pagers );
foreach ($children as $child) {
$permalink = get_permalink($child->ID);
$class = "page_item page-item-".$child->ID;
print('<li class="'.$class.'"><a href="'.$permalink.'">'.$child->post_title.'</a></li>');
}
print('</ul>');
}
print('</li>');
}
?>
</ul>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment