Support » Themes and Templates » How to show child pages from a parent page automatically?

  • Resolved delFUEGO


    I have this pages…

    –Day 1
    –Day 2
    –Day 3

    So, I need to show inside “Camp” some sort of index listing the “days”. So I created a new template page and used the “child_of=ID” template tag. But if I create a second “Camping” page, I would need another template page with a diferent ID in the “child_of” template tag.

    Is there a way of showing the child pages that belong to the parent automatically without writing parents ID so I can use the same template page with every new camp?

Viewing 13 replies - 1 through 13 (of 13 total)
  • So you’re hardcoding the ID in that string, right? As in “child_of=23” or something like that, correct?

    Instead, grab the ID dynamically…

    $g_page_id = $wp_query->get_queried_object_id();

    and use this variable, $g_page_id, throughout your page. Such as:

    wp_list_pages(“depth=1&child_of=”.$g_page_id .”&title_li=”);

    Never hard code ‘magic numbers’ into your site — use constants etc, but avoid ever having “where something = 567” because it’s too hard to recall where the number originated and difficult to maintain.

    Thank you very, very much carterco. That was just what I was looking for. It works lovely.


    thanks carterco! I’ve been searching for something very much like this over the last few hours.

    works exactly as I need it too!

    carterco, I just used your code above on a theme I’m working on . . . and it worked like a charm. I was trying to figure out how to “dynamically” call subpages when viewing a page. Very slick.

    Also, I wrapped your code above with a if (is_page()) { call, so it only loads when that page is viewed.

    I also wrapped the code in a “if (is_page())”, but I have a problem.
    My template shows a box around the list of child pages, but if there aren’t any, it shows an empty box anyway. How can I “bypass” the “show child pages” code if there are no child pages?

    This is the code Im using in the sidebar:

    <?php if (is_page()) { ?>
    <?php $g_page_id = $wp_query->get_queried_object_id(); ?>
    <?php the_title('<h2>', '</h2>'); ?>
    <?php wp_list_pages("depth=1&title_li=&child_of=".$g_page_id."&sort_column=menu_order"); ?>
    <?php } ?>

    from what you’ve said, I assume it’s CSS that’s putting the box around the children, and this was something you specifically wanted.

    i’m not sure how you would code it, but you would probably need to query the database to get some info.

    The best way would be to take the page id and query the database field to find the number of pages with a parent id that is equal to the current page id.

    Using an if function, you could then setup two lists, one if the number of children is 0 and another for if the number of children is >0.

    c_chana, yes, the box is made by CSS. What you suggested sounds like what im loooking for, but I have no clue on how to code it either.

    Does anybody know how to code it? Any help its widelly apreciated.

    And I’m still left wondering why after all this time there *still* isn’t a core function page_has_children() the takes a pageID as a parameter and returns True/False indicating if the page does or does not have child pages.


    ^ this was one of the first things I searched for when I first used wordpress and it seems that there are many others doing the same.


    the code structure would be something like this:

    $children = mysqlquery();

    if children == 0 {
    //Maybe add some text here?!
    } else {
    //Your original sidebar code would go here.

    You would need to run the query to see if there were any children of the page.

    Next, you would use an if statement to determine the outcome. The easiest would be to say, if the number of children ($children) = 0, then do nothing, in all other cases, run the sidebar menu script.

    I have the mysql query somewhere, I just can find it! when I do I’ll post it here.

    Oh, and please excuse my coding as I’m still new to php and wordpress!

    Thanks, Im looking forward that query 😀

    I have the following hierachy, where Page# is a horizontal menu, and *sub_Page are all located in a vertical menu in the sidebar.php template file.

    > Page1
    >> sub_Page1
    >>> sub_sub_Page1
    >> sub_Page2
    >>> sub_sub_Page1
    > Page2

    The code above works fine until I click on any ‘sub_Page#’ the vertical menu changes to:

    >>> sub_sub_Page

    How do I keep the vertical menu under the ‘Page#’ to show all the time?

    Here is my template code:

    if (is_page())
    $g_page_id = $wp_query->get_queried_object_id();

    Any help would be appreciated!

    I think that if you want to show always the same list, no matter where are you inside the page, then you should use the “wp_list_pages” template tag as usual, without the “g_page_id” trick.

    Maybe this will help some of you too.
    I figured out how to how, see this thread for the solution:

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How to show child pages from a parent page automatically?’ is closed to new replies.