Skip to content

Instantly share code, notes, and snippets.

@joshapgar
Last active April 14, 2017 12:49
Show Gist options
  • Save joshapgar/d9ff069f50e8b8bb78e669021237af75 to your computer and use it in GitHub Desktop.
Save joshapgar/d9ff069f50e8b8bb78e669021237af75 to your computer and use it in GitHub Desktop.
Dynamically reorder side navigation based on current page
<?php
/*******************************
* SIDEBAR NAVIGATION
*******************************/
function check_for_page_tree() {
//start by checking if we're on a page
if( is_page() ) {
global $post;
// next check if the page has parents
if ( $post->post_parent ){
// fetch the list of ancestors
$parents = array_reverse( get_post_ancestors( $post->ID ) );
// get the top level ancestor
return $parents[0];
}
// return the id - this will be the topmost ancestor if there is one, or the current page if not
return $post->ID;
}
}
function sidebar_navigation() {
// don't run on the main blog page
if ( is_page() ) {
// run the tutsplus_check_for_page_tree function to fetch top level page
$ancestor = check_for_page_tree();
// set the arguments for children of the ancestor page
$args = array(
'child_of' => $ancestor,
'depth' => '0',
'title_li' => '',
'sort_column' => 'menu_order',
);
// set a value for get_pages to check if it's empty
$list_pages = get_pages( $args );
// open a list with the ancestor page at the top
?>
<div class="sidebar-title">
<a href="<?php echo get_permalink( $ancestor ); ?>"><?php echo get_the_title( $ancestor ); ?></a>
</div>
<?php
// check if $list_pages has values
if ( $list_pages ) {
$currentPage = get_the_ID();
?>
<ul class="page-tree">
<?php
// use wp_list_pages to list subpages of ancestor or current page
//wp_list_pages( $args );
$childOrParent = child_or_parent();
switch ($childOrParent) {
case 'parent':
wp_list_pages(array('child_of' => $ancestor, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
break;
case 'childnokids':
wp_list_pages(array('include' => $currentPage, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
wp_list_pages(array('child_of' => $ancestor, 'exclude' => $currentPage, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
break;
case 'greatgreatgrandchildwithkids':
$postParent = wp_get_post_parent_id($currentPage);
$grandParent = wp_get_post_parent_id($postParent);
$greatGrandParent = wp_get_post_parent_id($grandParent);
wp_list_pages(array('include' => $grandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
<ul class="children" style="display: block">
<?php
wp_list_pages(array('child_of' => $grandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
</ul>
<?php
wp_list_pages(array('child_of' => $ancestor, 'exclude' => $grandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
break;
case 'greatgrandchildwithkids':
$postParent = wp_get_post_parent_id($currentPage);
$grandParent = wp_get_post_parent_id($postParent);
$greatGrandParent = wp_get_post_parent_id($grandParent);
wp_list_pages(array('include' => $grandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
<ul class="children" style="display: block">
<?php
wp_list_pages(array('child_of' => $grandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
</ul>
<?php
wp_list_pages(array('child_of' => $ancestor, 'exclude' => $grandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
break;
case 'grandchildwithkids':
$postParent = wp_get_post_parent_id($currentPage);
$grandParent = wp_get_post_parent_id($postParent);
$greatGrandParent = wp_get_post_parent_id($grandParent);
wp_list_pages(array('child_of' => $ancestor, 'exclude' => $greatGrandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
break;
case 'child':
$postParent = wp_get_post_parent_id($currentPage);
wp_list_pages(array('include' => $postParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
<ul class="children" style="display: block">
<?php
wp_list_pages(array('child_of' => $postParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
</ul>
<?php
wp_list_pages(array('child_of' => $ancestor, 'exclude' => $postParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
break;
case 'childwithkids':
$postParent = wp_get_post_parent_id($currentPage);
//wp_list_pages(array('include' => $postParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
<ul class="children" style="display: block">
<?php
wp_list_pages(array('child_of' => $postParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
</ul>
<?php
//wp_list_pages(array('child_of' => $ancestor, 'exclude' => $postParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
break;
case 'greatgreatgrandchildnokids':
$postParent = wp_get_post_parent_id($currentPage);
$grandParent = wp_get_post_parent_id($postParent);
$greatGrandParent = wp_get_post_parent_id($grandParent);
wp_list_pages(array('include' => $greatGrandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
<ul class="children" style="display: block">
<?php
wp_list_pages(array('child_of' => $greatGrandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
</ul>
<?php
wp_list_pages(array('child_of' => $ancestor, 'exclude' => $greatGrandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
break;
case 'greatgrandchildnokids':
$postParent = wp_get_post_parent_id($currentPage);
$grandParent = wp_get_post_parent_id($postParent);
$greatGrandParent = wp_get_post_parent_id($grandParent);
wp_list_pages(array('include' => $grandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
<ul class="children" style="display: block">
<?php
wp_list_pages(array('child_of' => $grandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
</ul>
<?php
wp_list_pages(array('child_of' => $ancestor, 'exclude' => $grandParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
break;
case 'grandchildnokids':
$postParent = wp_get_post_parent_id($currentPage);
$grandParent = wp_get_post_parent_id($postParent);
wp_list_pages(array('include' => $postParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
<ul class="children" style="display: block">
<?php
wp_list_pages(array('child_of' => $postParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
?>
</ul>
<?php
wp_list_pages(array('child_of' => $ancestor, 'exclude' => $postParent, 'title_li' => '', 'sort_column' => 'menu_order', 'depth' => '0'));
break;
default:
# code...
break;
}
?>
</ul>
<?php
}
}
}
/***************************************
*BONES CHECK IF PAGE is CHILD OR PARENT
***************************************/
function child_or_parent(){
global $post;
$parent = get_post_ancestors($post->ID);
if( is_page() && $post->post_parent > 0 ) {
// post has parents
$children = get_pages('child_of='.$post->ID);
if( count( $children ) != 0 && !empty($parent[3])) {
return "greatgreatgrandchildwithkids";
}
if( count( $children ) != 0 && !empty($parent[2])) {
return "greatgrandchildwithkids";
}
if( count( $children ) != 0 && !empty($parent[1])) {
return "grandchildwithkids";
}
if( count( $children ) != 0 && empty($parent[1])) {
return "childwithkids";
}
if ( is_page() && (count(get_post_ancestors($post->ID)) >= 4) ) {
return "greatgreatgrandchildnokids";
}
if ( is_page() && (count(get_post_ancestors($post->ID)) >= 3) ) {
return "greatgrandchildnokids";
}
if ( is_page() && (count(get_post_ancestors($post->ID)) >= 2) ) {
return "grandchildnokids";
}
if( count( $children ) <= 0 && empty($parent[1]) ) {
return "childnokids";
} elseif ( count( $children ) <= 0 && !empty($parent[1]) ) {
return "child";
}
} else {
return "parent";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment