Adding Sub Page Links to Parent Pages in WordPress

Teli Adlam —  December 11, 2007

Sometimes it’s just not feasible to have the sub (or child) pages listed within your sidebar in WordPress, especially if you have a lot of child pages. However, instead of needing to manually add each link to its parent page, you can quickly do it with a bit of PHP goodness. Using the following code within your page.php file will add a list of all the sub pages to the currently viewed parent page.

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

Please note that the above code must be used within the WordPress loop in order to work. (I personally prefer to put it right after <?php the_content(); ?> so it will display the list of sub pages after the page content.)

To remove the sub pages from showing under the parent pages in the sidebar, add ‘depth=1’ in addition to any other variables in the wp_list_pages() code within your sidebar. Example: <?php wp_list_pages(‘title_li=&depth=1’); ?>

0 Shares

Teli Adlam

Posts

14 responses to Adding Sub Page Links to Parent Pages in WordPress

  1. Now that was some useful information. I am a php noob so any code hacks or tricks are definitely useful information.

  2. SEOjr Search Engine Optimization December 16, 2007 at 12:52 am

    Very useful; I’ll try it on one of my blogs. Thanks.

  3. This is the one area were WordPress lacks for CMS capability and this lil hack will help hopefully!!

  4. Hey Teli, two questions on coding edits in WordPress. First, I don’t know how to actually get into any of the files to edit them so if you could tell me how that’s done that would help me address these items.

    1. How do I add my picture to the header or one of the side columns?

    2. How do I install a simple post tagging feature that becomes part of the template at the bottom of each post so I can custom tags to each post.

    Thanks for your help.

  5. Whoops, I don’t think I clicked the follow up with comments with e-mail check box so please shout back at me that way too.

    Thanks…..martin

  6. Whilst that’s a nice WordPress hack, I’ve been nervous of hacking my own blog too much, due to the issues of upgrading later. Do you have any advice on how to keep track of hacks like this and how to incorporate them into future wordpress upgrades?

  7. I do prefer some kind of plugin for this too. Since I’m a noob and it looks like you know much more about it :), is it possible for you to make such a plugin ?

    Dave

  8. Perfect Wealth Formula February 29, 2008 at 4:21 pm

    Thanks for the code. I am with a PP, This noob appreciates the help and insight.

  9. Thanks for the tips and I had it on my sidebar. Just that I fail to get the subpages indented….:(

  10. Great little script.

    When cutting and pasting from this webpage, watch out that the single quotes come through ok. I could not make this work at first then I noticed that the single quotes looked unusual – changed them all and it then worked fine.

    Thanks

  11. Hi,

    I couldn’t get his code to work, does it work on wordpress 2.6.2?

  12. There is a subpage widget now in the plug-ins directory at WordPress.

  13. Hi, I have similar code to this. Where my problem lays.. is when your go to the third child of a post.

    Here’s an example:

    About us – > Our Team -> CEO So & So

    The links stay up when on about us and our team, but disappear when going to the third depth.

    Any help?

  14. Thanks for the tip but I had a problem it gave an error:

    Parse error: syntax error, unexpected ‘=’

    for line:

    $children = wp_list_pages(’title_li=&child_of=’.$post->ID.’&echo=0′);

    If anybody can help?????