WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to show child pages from a parent page automatically? (14 posts)

  1. delFUEGO
    Member
    Posted 8 years ago #

    I have this pages...

    Events
    -Party
    -Camp
    --Day 1
    --Day 2
    --Day 3
    -Meeting

    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?

  2. Buz Carter
    Member
    Posted 8 years ago #

    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.

  3. delFUEGO
    Member
    Posted 8 years ago #

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

    RESOLVED!

  4. c_chana
    Member
    Posted 8 years ago #

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

    works exactly as I need it too!

  5. scottwallick
    Member
    Posted 8 years ago #

    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.

  6. delFUEGO
    Member
    Posted 8 years ago #

    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>'); ?>
    <ul>
    <?php wp_list_pages("depth=1&title_li=&child_of=".$g_page_id."&sort_column=menu_order"); ?>
    </ul>
    <?php } ?>

  7. c_chana
    Member
    Posted 8 years ago #

    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.

  8. delFUEGO
    Member
    Posted 8 years ago #

    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.

  9. TechGnome
    Moderator
    Posted 8 years ago #

    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.

    -tg

  10. c_chana
    Member
    Posted 8 years ago #

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

    delFUEGO,

    the code structure would be something like this:

    <?php
    $children = mysqlquery();

    if children == 0 {
    //DO NOTHING!
    //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!

  11. delFUEGO
    Member
    Posted 8 years ago #

    Thanks, Im looking forward that query :D

  12. strugglefish
    Member
    Posted 8 years ago #

    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();
    wp_list_pages("depth=4&title_li=&child_of=".$g_page_id."&sort_column=menu_order");
    }

    Any help would be appreciated!

  13. delFUEGO
    Member
    Posted 8 years ago #

    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.

  14. strugglefish
    Member
    Posted 8 years ago #

    Maybe this will help some of you too.
    I figured out how to how, see this thread for the solution:
    http://wordpress.org/support/topic/32422

Topic Closed

This topic has been closed to new replies.

About this Topic