How to: Display subpages on your sidebar

If you use pages and subpages on your blog, it can be a good idea to use your sidebar to create a menu of subpages. If you’d like to try, the following code is what you need!

In order to achieve this recipe, place the following code on your sidebar.php file.

<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) { ?>
    <ul>
        <?php echo $children; ?>
    </ul>
<?php } ?>

The above code executes the wp_list_pages function, with the echo=0 parameter, which allow us to get the result of the function without displaying it. Then, we test if the $children variable isn't empty, and we display our children pages.

Credit goes to Kyle Eslick and his awesome blog wphacks.com for this excellent recipe!

Related Posts

No related posts.

2 Responses

Jul 20 2011 00:31

Thanks for the code! How do I display the parent page as the first item in the list? Right now, this code only displays the child pages.

Nov 19 2011 18:13

Hello, thanks for your title..
I have problem, friend. I want to show the current parent title and children title in sidebar continuously, based on page active.

Example :

My Page :
Info
> Contact

If i selected Contact, i want to show parent title ‘Info’ too. So, the display like this :

INFO
– Contact

Help me.. please.

Thanks before

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required