Skip to content

Instantly share code, notes, and snippets.

@rbk
Created July 8, 2015 18:53
Show Gist options
  • Save rbk/bfe368e2fb952b2f4360 to your computer and use it in GitHub Desktop.
Save rbk/bfe368e2fb952b2f4360 to your computer and use it in GitHub Desktop.
Wordpress two level sidebar
<?php
$sidebar = false;
$subpages = get_posts(array('post_type'=>'page', 'post_parent'=>$post->ID));
if( count($subpages) > 0 ){
foreach( $subpages as $sp ){
if( $sp->ID == $post->ID ){
$sidebar .= '<div><a class="active" href="'.$sp->guid.'">'.$sp->post_title.'</a></div>';
} else {
$sidebar .= '<div><a href="'.$sp->guid.'">'.$sp->post_title.'</a></div>';
}
}
} else {
if( $post->post_parent != 0 ){
$subpages = get_posts(array('post_type'=>'page', 'post_parent'=>$post->post_parent));
if( count($subpages) > 0 ){
foreach( $subpages as $sp ){
if( $sp->ID == $post->ID ){
$sidebar .= '<div><a class="active" href="'.$sp->guid.'">'.$sp->post_title.'</a></div>';
} else {
$sidebar .= '<div><a href="'.$sp->guid.'">'.$sp->post_title.'</a></div>';
}
}
}
}
}
echo ($sidebar)?$sidebar:''; // if exists
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment