Skip to content

Instantly share code, notes, and snippets.

@wpmark
Created October 20, 2013 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmark/7068116 to your computer and use it in GitHub Desktop.
Save wpmark/7068116 to your computer and use it in GitHub Desktop.
WordPress Sidebar Menu for Pages
<?php
/* check whether the current post has a parent */
if( $post->post_parent ) {
/* get the ancestors/parent of the current post */
$pxjn_ancestors = get_post_ancestors( $post->ID );
/* count the number of parents retrieved less 1 */
$pxjn_root = count( $pxjn_ancestors ) -1;
/* set the parent to the */
$pxjn_parent = $pxjn_ancestors[ $pxjn_root ];
/* current post has no parent */
} else {
/* set current post id as post parent */
$pxjn_parent = $post->ID;
} // end if current post has parent
/* set the title variable to the post parent page */
$pxjn_title = wp_list_pages( 'title_li=&echo=0&include='.$pxjn_parent );
/* get the children of the current post */
$pxjn_children = wp_list_pages( 'title_li=&child_of='. $pxjn_parent .'&echo=0&depth=1&orderby=menu_order&order=ASC' );
/* if any children are returned/present */
if ($pxjn_children) {
/* output some html to wrap our menu in */
echo '<nav class="subnav"><ul id="nav" class="subnav-menu">';
/* output the parent page title and the child pages as list items */
echo $pxjn_title; echo $pxjn_children;
/* close our html tags for the list */
echo '</ul></nav>';
} // end if page has children
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment