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.

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

Jul 21 2009 10:32

That’s the code I’ve been looking for. Thank you very much. I’ll implement it in the near future

Regards

Peter

Jul 23 2009 10:58

Hi Jean,

Great recipe. Two questions:

1. How do you adjust the code to still show only the subpages for the parent page the user is on? For example, if the parent page is “Trees”, and the user is on that page, I’d like the sidebar to show ONLY the sub pages for that parent page.

2. Related to #1. How do you display the list of subpages, even if you are on a subpage. Using the above example, the user could be on a subpage called “Oak Trees”, but I still want all the subpages to show (“Oak Trees, Spruce Trees, etc”.

Thanks!

Nov 26 2009 17:20

Having just started out in designing & coding my own WP template, WP Recipes has been a tremendous help! Thank you very much for all the great hacks and tips!

I have the following query:

I’ve used the above code with great success to create a “children menu” in the content of my ABOUT page. Whenever I’m in the About page, it shows the page’s children. When I click on a child, that child’s children are then displayed. As soon as I reach the “lowest” child, the menu disappears, which is just fine.

Is it possible to reverse the above results, so that when you are on a child page, that child’s parent show up in a(nother) menu bar, but only until before you reach the top level menu? This will result in you having a “parent menu”, as well as a “children menu”.

I’ve altered the above code and it works just fine, but I have no clue how to make the “parent menu” disappear as soos as it returns the top level menu.

Any help would be dearly appreciated!

Jan 04 2010 10:41

its not very usefull, when i click one subpage in the sidebar, when it goest to the link the sidebar disapear =x

Jan 06 2010 17:44

Alon, I’m with you, trying to come up with a menu to create link lists in the WordPress sidebar that are per page based. Let me know if you unravel it?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required