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 Recipes

  • No related recipes.

20 Responses

Oct 31 2008 09:48

Hi Jean,
thanks for recipe..
is it possible to show your recipes’ demos or screenshots in a specific page or here?
so let us know what we will see before applying code..
Cheers~

Oct 31 2008 17:18

I’d love to upload exemples for my recipes, but sadly it will probably take too much time and I got 2 other blogs, so if I want to continue to publish one recipe per day, I don’t think I’ll be able to do this :(

Nov 01 2008 00:41

ok,you r right Jean,
i prefer one recipe per day:)
take it easy
cheers~

Nov 01 2008 21:46

Thats called remix recipe.
Bcoz we are reconstructing our blog using these subpages links

Nov 02 2008 04:42

yeah right maintaining many blogs is consuming time …

Nov 02 2008 09:38

once again, add some screenshot brother
byme

Nov 02 2008 11:18

Trues why dont you put some pictures???

Nov 02 2008 11:38

@byme & Evan: As I said, I don’t have enought time to upload exemple or screenshots of recipes, unless it is necessary.
However, I keep your suggestion in mind, maybe I’ll do that later :)

Nov 05 2008 14:37

The if statement’s not really that necessary - if no subpages exist, you’ll just have an empty unordered list. But I suppose it is a bit cleaner.

Dec 05 2008 01:16

Thanks for this code, it works great and is self explanatory. Why is a screenshot needed?

Question: Would it be possible to have WP sidebar always display all subpages, regardless of the main page?

Jan 13 2009 05:14

I agree about adding some picture and make us see how it looks when we implement this code.

Feb 24 2009 15:52

This is a nice simple solution - one thing I noticed is that if the child page has subpages they display also.

I wonder if it’s possible to limit it to immediate children only?

Feb 26 2009 15:15

I’ve added the code to my Sidebar.php and It doesn’t return anything. I see nothing on my page.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required
Blog And Make Cash