DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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!

3 Comments

  1. 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.

  2. 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

  3. It works, thanks for recipe 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *