Skip to content

Instantly share code, notes, and snippets.

@rodica-andronache
Created October 11, 2013 20:15
Show Gist options
  • Select an option

  • Save rodica-andronache/6941312 to your computer and use it in GitHub Desktop.

Select an option

Save rodica-andronache/6941312 to your computer and use it in GitHub Desktop.
WORDPRESS - Show children of page if page is parent, and siblings if page is not a parent
<?php
global $post; // if outside the loop
if ( is_page() && $post->post_parent ) {
// This is a subpage
$mypages = get_pages( array( 'child_of' => $post->post_parent, 'sort_column' => 'post_date', 'sort_order' => 'asc', 'hierarchical' => '0', 'parent' => $post->post_parent ) );
echo '<ul id="nav">';
foreach( $mypages as $page ) {
if($page->ID == $post->ID)
echo '<li><a href="'.$page->guid.'" class="active">'.$page->post_title.'</a></li>';
else
echo '<li><a href="'.$page->guid.'">'.$page->post_title.'</a></li>';
}
echo '</ul>';
} else {
// This is not a subpage
$mypages = get_pages( array( 'child_of' => $page_id, 'sort_column' => 'post_date', 'sort_order' => 'asc', 'hierarchical' => '0', 'parent' => $page_id ) );
echo '<ul id="nav">';
foreach( $mypages as $page ) {
echo '<li><a href="'.$page->guid.'">'.$page->post_title.'</a></li>';
}
echo '</ul>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment