• Resolved dragongamer54

    (@dragongamer54)


    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;">&nbsp;</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)
  • Thread Starter dragongamer54

    (@dragongamer54)

    Omg, Sorry for posting the earlier thread. The solution was so simple!
    This is what I did (for those having the same problem).

    I only changed the order of the code. My code is now as follows:

    <?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) { ?>
    <div id="sidebar">
        <ul>
            <li>
                <div style="clear: both;">&nbsp;</div>
            </li>
            <li>
    
                <ul>
                    <?php echo $children; ?>
                </ul>
            </li>
            <li> </li>
        </ul>
    </div>
    <?php } ?>

Viewing 1 replies (of 1 total)
  • The topic ‘Only show sidebar when subpages’ is closed to new replies.