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!
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!
25 Responses
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~
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
ok,you r right Jean,
i prefer one recipe per day:)
take it easy
cheers~
Thats called remix recipe.
Bcoz we are reconstructing our blog using these subpages links
yeah right maintaining many blogs is consuming time …
once again, add some screenshot brother
byme
Trues why dont you put some pictures???
@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
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.
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?
I agree about adding some picture and make us see how it looks when we implement this code.
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?
I’ve added the code to my Sidebar.php and It doesn’t return anything. I see nothing on my page.
That’s the code I’ve been looking for. Thank you very much. I’ll implement it in the near future
Regards
Peter
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!
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!
its not very usefull, when i click one subpage in the sidebar, when it goest to the link the sidebar disapear =x
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: