Skip to content

Instantly share code, notes, and snippets.

@dholmes
Created July 13, 2012 05:34
Show Gist options
  • Save dholmes/3102932 to your computer and use it in GitHub Desktop.
Save dholmes/3102932 to your computer and use it in GitHub Desktop.
[wp-snippet] sidebar navigation
<?php
//if the post has a parent
$title = "";
global $post;
if($post->post_parent){
//collect ancestor pages
$relations = get_post_ancestors($post->ID);
//add current post to pages
array_push($relations, $post->ID);
$posts = get_pages("echo=0&sort_column=menu_order&child_of=".$post->ID);
foreach($posts as $page){
array_push($relations, $page->ID);
}
//get comma delimited list of children and parents and self
$relations_string = implode(",",$relations);
//use include to list only the collected pages.
$sidelinks = wp_list_pages("title_li=&echo=0&sort_column=menu_order&include=".$relations_string);
}else{
// display only main level and children
$sidelinks = wp_list_pages("title_li=&echo=0&sort_column=menu_order&depth=0");
}
?>
<h1><?=$post->post_title?></h1>
<?php
if ($sidelinks) { ?>
<ul class="navmenu">
<?php //links in <li> tags
echo $sidelinks; ?>
</ul>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment