• Hi all,
    I’m having some trouble displaying the Child Pages of the Current Page in our Sidebar Menu. We have a hierarchy on our site like this:

    Classrooms (main menu link)
    > First Grade
    > Introduction
    > Second Grade
    > Special Activities
    > Third Grade
    > Introduction
    > Homework

    When a user clicks the ‘Classrooms’ link, the sidebar menu displays the Child Pages fine like this:
    First Grade
    Second Grade
    Third Grade

    However, when the user clicks the ‘Third Grade’ link, we want it to show its own Child Pages, but instead it shows the same list of pages: First Grade, Second Grade, Third Grade list as the ‘Classrooms’ link above.

    When the user clicks the ‘Homework’ link (child of Third Grade), the sidebar menu lists the Third Grade child pages, which we like, fine like this:
    Introduction
    Homework

    Here is the code we currently have in our sidebar:
    $children_pages = get_pages( array( 'child_of' => $child_of, 'parent' => $child_of, 'sort_column' => 'ID', 'sort_order' => 'ASC' ) );

    So the problem lies in the ‘Third Grade’ level link. We want to change this so when the user clicks the ‘Third Grade’ link, the sidebar menu displays the ‘Third Grade’ Child Pages (ie: Introduction & Homework) and not list First Grade, Second Grade, Third Grade.

    I tried changing $child_of, 'parent' to $child_of, 'current page', but displays all of the child pages and their sub-pages together like this:
    First Grade
    Introduction
    Second Grade
    Special Activities
    Third Grade
    Introduction
    Homework

    Here is where you can view it: https://www.stagnesavon.org/classroom-pages/

    Can anyone share how I can accomplish this?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Setting both ‘child_of’ and ‘parent’ arguments to the same value is the correct way to display only immediate children of that value. It appears to only do that for the top level page. Even though you like the results when ‘Homework’ is loaded, it is wrong, there should be no children of this level.

    I have to suspect the method of getting $child_of is flawed. Once it is working correctly, there will be nothing shown at the ‘Homework’ level, but you could add some sort of code that amounts to “If nothing is returned, get immediate children of the parent (get siblings).”

Viewing 1 replies (of 1 total)
  • The topic ‘List Child Pages of Current Page with get_posts’ is closed to new replies.