solostream

List only child Pages of a specific parent Page

| May 9, 2007 | 45 Comments

I am building a site for a client, and I want to list all the child Pages, which will be many, of one of their parent pages in the sidebar. To help you understand better, this client provides many services, so I wanted there to be a parent Page called “Services,” and this parent Page will have a list of child Pages, each of which goes into detail about each specific service.

I couldn’t find any information on how to do this, but then I came across a tag for categories that allows you to only list the subcategories of a specific category. This tag is as follows:

<?php wp_list_cats('child_of=8, 14'); ?>

By using this tag, you would only list the child categories of the categories with IDs of 8 and 14. So I decided to see if this would work with Pages, and it did!

So here’s the code I put in the sidebar to list only the child Pages of the Services Page, sorted alphabetically and omitting the title of “Pages”:

<?php wp_list_pages('child_of=2&sort_column=post_title&title_li=') ?>

Hurray!

Tags:

Category: WordPress as CMS

Elegant WP Themes

Comments (45)

Trackback URL | Comments RSS Feed

  1. Brad says:

    FABULOUS WORK!!

    I have never been to this site before, but your most recent insight into listing the child pages to a specific parent page works GREAT!

    Wonderful in sharing your findings.

  2. Miriam says:

    Thanks Brad! I’m glad that this information helps you. Hope to see you again!

  3. Glen says:

    Nice spot, been trying to figure this out all day before stumbling upon your post! Many thanks for the info.

  4. Miriam says:

    Glen – glad to help!

  5. Alvin says:

    I’m trying to do this within a page rather than on the sidebar. I tried the code, but it didn’t work. Any thoughts?

  6. Alvin says:

    Hey, I figured it out. I had to put the code in a php file, not in the actual page. I’m just an attorney, so it takes me a while to figure things out. But the code works. Thanks.

  7. Miriam says:

    Alvin – don’t be too hard on yourself – lawyers can do anything! That’s why we all love them.

    Glad you figured it out and that it has helped you!

  8. Luigi says:

    Is there a way to categorize pages or sub-pages, and be able to place those categories in a drop down menu to select different sub-pages ?

  9. Miriam says:

    Hi Luigi – cool blog! Do you want to be able to place Pages – i.e. the pages on a blog that are outside of the blogging loop, like the About page here for example – in categories? If so, I’ve never come across a way to do that. If you want to create drop-down menus for categories, I have seen plugins that do that kind of thing. Try searching on Google. If I come across those plugins, I’ll try to remember to let you know.

  10. Luigi says:

    thanks Miriam.
    That was my prob….I know how to do create the drop down menu for the normal categories.

    But in WP, they are only associated to posts and not to pages.

    I need to categorize pages to make the Album pages available for browsing not only by genre (classical or Jazz) but also for kind of music ensamble (quartet, quintet, orchestra).
    This could be sub-categories of the main categories: jazz and classical

  11. Miriam says:

    Good news Luigi! I just came across a plugin that enables you to categorize Pages: Page Category Organiser. I probably would have ignored it if you hadn’t asked your question.

    I haven’t tested it out, so I don’t know how well it works, so please can you let me know once you’ve tried it out? Thanks.

  12. Luigi says:

    Hi Miriam,
    I already tested that plugin.
    It has some bugs …. and it’s only possible to assign one category to each page.

    I need to give more than one :(

    More, it does not seem to create permanent links for page category….

  13. sebas says:

    great, but how can i only display only first childs of that page??

    depth=1

  14. Anne says:

    Thank you, that worked like a charm!

  15. Miriam Schwab says:

    Hey Anne – glad to help.

  16. Worked like a charm for me too – thanks Miriam!

  17. Remon says:

    Great article… it helped me figure out a different problem.  Thanks

  18. finally!
    thanks so much man, i was looking for this for a while and it turns out to be really simple ;)

    thanks

  19. Miriam Schwab says:

    You’re welcome Dejan! Glad to help.

  20. Emily says:

    Thank you, this was very helpful!  I thought I might also offer the additional tweak I wound up with, for other readers: making the parent page the title list item.  It turns out to be just one extra line of code.

    <code>$home = wp_list_pages(‘include=483&title_li=&echo=0′);
    wp_list_pages(“child_of=483&title_li=” . $home  );</code>

    Cheers

  21. [...] List only child Pages of a specific parent Page – how to list all the child Pages of the current page in the sidebar [...]

  22. olof says:

    hi!
    please, help me: how can i display children page only when click on link of parent page? — i want only parent pages to be displayed when generates list of pages

  23. Susan says:

    This is exactly what I was looking for!

  24. miles says:

    Hi,
    I haven’t tried out your solution yet, or the subsequent ones either, I’m wondering if this thing can be applied to a SPECIFIC page, and not all pages.  Because its not clear if this solution is for all pages…which I don’t want.  I want it for one specific page.
    Thanks!

  25. paul says:

    just wanted to say that i’ve come across your site numerous times after searching google for certain solutions and every time, i find what i’m looking for here.   it’s got to the point where i see your site come up, i pretty much know whatever you suggest in  a post will work. this post is a great example. thanks!

  26. Thanks so much for the positive feedback Paul! I’m so glad that you find our posts useful.

  27. Pete says:

    wow wow wow lol many thanks, exactly what i’ve been searching for a couple days now :D

  28. ovidiu says:

    any idea if I can make the output wrap every child into a <h4> tag instead of a <li> tag?

  29. Mer says:

    I just struggled with this for an hour, and you gave me the clue I needed. Thanks!
    If you want a page to show a list of it’s own children, this is the template/theme code:
    <?php wp_list_pages(‘child_of=’.$post->ID.’&sort_column=post_title&title_li=’) ?>

  30. pantone204 says:

    You saved my sanity. I’ve been trying to figure this out for 2 days. Thank you thank you thank you.

  31. Ha! Nice work Miriam :) Glad I landed up here early hehe.

  32. Angela Jude says:

    Awesome tip. Posted quite awhile ago but still helping people out! Thank you!

  33. Mike says:

    You are a lifesaver!! I have been looking for this code for the past 3 hours! Thank you very much!

  34. Justin Sternberg says:

    just what i needed…. although… is there a way to use the page name rather than the page id?  the reason is, i test it out locally via MAMP, and then when i upload to server, i have to change all of the ids to match the page ids on the server… keeps it from being able to be synchronized.

  35. VaskarG says:

    Thank you,
    I found, what I am searching?

  36. Paul says:

    Hi
    I got this working no problem, thanks a lot. However, I would also be wanting to add <strong></strong> elements within the link for the child page. So..
    <ul><li><a href=”#”><strong>Child Page</strong></li></ul>
    Any chance you would know how to do this?
    Thanks in advance!

  37. Webcreationz says:

    I really have been busy for 2 full hours to get the subpage shown on the parent page. All the other forums weren’t doing the trick, because the ‘while($recent->have_posts..’ was blocking somethng.

    So after 2 hours, your simple code worked for us!

  38. Gray says:

    @ Justin Sternberg- if you got to Settings> Permalinks, you have a choice to explicitly set the id’s of the pages so the migration should be very easy.
     

  39. Shawn Grimes says:

    Thank you very much for this useful tip! I have a secondary navigation in my footer that lists all the child pages and I was searching for a way to make it dynamic as well. Well done!

  40. Thank you a ton! That helped me out perfectly!!

  41. @Gray… thank you for your reply… i checked the permalinks settings but do not see that option to explicitly set the id’s of the pages.  see screenshot
    —> http://drp.ly/1vKC5s
     

  42. Gray says:

    Justin Sternberg from  what I understood you wanted to change the default id’s to the page names, right? Well, from the screenshot you just sent, if you select Custom Structure, that will automatically change the ids to the names of your page. I found that /%category%/%postname%/ was most ideal.
    Hope that helps!
     

  43. @Gray ok, now i understand what you’re saying…  thank you!  so, to get this to work, replace:

    <ul class=”children”>
    <?php
    if($post->post_parent)
    $children = wp_list_pages(“title_li=&child_of=”.$post->post_parent.”&echo=0″);
    else
    $children = wp_list_pages(“title_li=&child_of=”.$post->ID.”&echo=0″);
    if ($children) {
    ?>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>
    </ul>
    with:

    <ul class=”children”>
    <?php
    if($post->post_parent)
    $children = wp_list_pages(“title_li=&child_of=”.$post->post_parent.”&echo=0″);
    else
    $children = wp_list_pages(“title_li=&child_of=”.$post->MyPageName.”&echo=0″);
    if ($children) {
    ?>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>
    </ul>
     
    ?

  44. @Gray ok, now i understand what you’re saying…  thank you!  so, to get this to work, replace:
    <?php wp_list_pages(‘child_of=2&sort_column=post_title&title_li=’) ?>

    with:
    <?php wp_list_pages(‘child_of=MyPageName&sort_column=post_title&title_li=’) ?>

    ?

  45. Bas says:

    Hi,
    i see you are the pages specialist ;)
    i have the following problem: I want to have the pages in another DIV then the subpages of the selected page. (see a drawing of the problem)
    I hope you can help,
    Bas

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.