Forums

[resolved] Problems listing all parent/ancestor pages (2 posts)

  1. liblogger
    Member
    Posted 3 years ago #

    Hi there. I'm trying display a list of pages that includes any given page's parent, siblings and children, something like this:

    • Parent
      • Page
        • Child 1
        • Child 2
      • Sibling

    I'm not able to use wp_list_pages in it's simplest form, because the Parent page also has siblings. I've been trying to use the example on the Template Tags/wp list pages page in the Codex, but, although it works fine for the parent and sibling pages, when I go to the child pages, only those child pages are listed, giving me:

    • Child 1
    • Child 2

    This is the code I'm currently using:

    <?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 } ?>

    Is it at all possible to adapt this code to take into account more than one level of children e.g. using 'ancestor' rather than 'parent'? (You can probably tell I know enough php to copy and paste without the template imploding, but not enough to reverse engineer the code myself :)

    Any help very much appreciated!

  2. liblogger
    Member
    Posted 3 years ago #

    Never mind, I decided to go with the Fold Page List plugin and used this code (adapted from the suggestions in this forum topic):

    <?php
    $g_page_id = $wp_query->get_queried_object_id();
    $ancestorIDs = _wswwpx_page_get_ancestor_ids($g_page_id);
    $titlePage = $ancestorIDs[1];
    $childIDs = _wswwpx_page_get_child_ids($titlePage);
    if (is_array($childIDs)) { ?>
    <ul>
    <?php
    $grandParent = $ancestorIDs[1];
    wswwpx_fold_page_list("title_li=&sort_column=menu_order&child_of=".$grandParent);
    ?>
    </ul>
    <?php } ?>

    Seems to work so far, although I need to work out the styling...

Topic Closed

This topic has been closed to new replies.

About this Topic