Forums

Problem with sub-pages, parents pages... (4 posts)

  1. Didier Sampaolo
    Member
    Posted 3 years ago #

    Hello all !

    I'm got a problem with some WordPress code. I hope i'll be clear enough for you to help me :)

    I've got a page structure like this :

    a -> b -> c,d,e
      -> f -> g,h,i

    I would like to have a "submenu" on my blog, depending on the parent (on depth 1) of the current page. IE, on page "d", i'd like to show the whole tree under the page "a" :
    a -> b -> c,d,e

    I guess i'll have to use wp_list_pages('child_of=a'); at some point, this part is ok ('a' being replaced by its ID).

    But, can I find a way to easily retrieve the ID of 'a' (the "grand-parent") when on page c or d ?

    Should I make a loop with something like "while parent page is not on depth 1, get the parent" ?
    (there can be more than 3 depths, but not so many, so the loop should be ok)

    If so, coud you please help me writing this loop ?
    HSome hints may be enough, I don't ask you to do all the work ;)

    Thanks a lot !

  2. Didier Sampaolo
    Member
    Posted 3 years ago #

    Ok I just found out a way to achieve this. Here is my code. Suggestions welcome :)

    `$id = get_the_ID();

    $pages = get_pages('depth=1');
    $topParent = null;
    foreach($pages as $page) {
    // as get_pages doesn't support Depth argument, we must check for page depth (no parent = depth 1)
    if ($page->post_parent == "0") {
    if ($page->ID == $id) {
    $topParent = $id;
    }
    }
    }

    if($topParent == null) {
    // searching in deep pages
    foreach($pages as $page) {
    // we'll only search in top-pages childs
    if ($page->post_parent == "0") {
    $pageID = $page->ID;
    $subpages = get_pages('child_of='.$pageID);

    foreach($subpages as $subpage) {
    if ($subpage->ID == $id) {
    // found as a child of $pageID
    $topParent = $pageID;
    }
    }
    }
    }
    }
    wp_list_pages('child_of='.$topParent);`

  3. Didier Sampaolo
    Member
    Posted 2 years ago #

    BTW, I just released it as a plugin named "wp_list_sub_pages"

  4. stephens01234
    Member
    Posted 2 years ago #

    Dsampaolo,
    I like your code and I would love to use it, help me out a bit.
    Here is what I am trying for. List all Parent pages, but only list a child page if one is on the parent of that child or a sister of the child.

    I installed your plugin, but I am unclear where I am supposed to put the <?php wp_list_sub_pages(); ?>
    I am operating in the sidebar.php page. When pasting `<?php wp_list_sub_pages(); ?>' minus the php tags, as you have done in the above tag, it does not work. Plugin activated or not.
    If you can just provide the code, without the plugin that would work for me too.

    Hope I make sense.

Topic Closed

This topic has been closed to new replies.

About this Topic