Only show sidebar when subpages
-
Hey there
I am making my own custom theme for wordpress but I have stumbled upon a little problem.
I want to make a sidebar which always shows the subpages of the parent page but if a parent page has no subpages then the sidebar needs to “dissapear” (No div) so my content can take the full page width.
How am I able to achieve this?
My sidebar code is as follow:
<div id="sidebar"> <ul> <li> <div style="clear: both;"> </div> </li> <li> <?php if(!$post->post_parent){ // will display the subpages of this top level page $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); }else{ // diplays only the subpages of parent level //$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); if($post->ancestors) { // now you can get the the top ID of this page // wp is putting the ids DESC, thats why the top level ID is the last one $ancestors = end($post->ancestors); $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0"); // you will always get the whole subpages list } } if ($children) { ?> <ul> <?php echo $children; ?> </ul> <?php } ?> </li> <li> </li> </ul> </div>
As you see, my current code only gives me the subpages when they exist but doesn’t let the sidebar div “dissapear” when there are no subpages.
Thanks in advance.
Greetings dragongamer54
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Only show sidebar when subpages’ is closed to new replies.