• PJ

    (@twothirty)


    i have a site where i’m trying to display JUST a list of the page children and grandchildren, with a heading at the top that’s the page parent.

    this isn’t too hard, but i want the list of the parent at the top, the children (and nested grandchildren) to show up on every page within that branch (i.e. on the parent page, the child pages, the grandchildren pages).

    i can get it work if it’s 2 levels, but if you click on the 3rd level deep, it shows child as the parent in the title.

    suggestions? is there a plugin or hack to do that?

    here’s the code:

    <div id=”subnav”>
    <?
    $thispage = $wp_query->post;
    $page = $wp_query->post;
    $parent_name = $wpdb->get_var(“SELECT post_name FROM $wpdb->posts WHERE ID = ‘$page->post_parent;'”);

    if ($thispage->post_parent!=0) { ?>

    <h2><? echo ($parent_name); ?></h2>

      <? wp_list_pages(“title_li=&child_of=”.$thispage->post_parent); ?>

    <? } else { ?>
    <h2><? echo ($thispage->post_name); ?></h2>

      <? wp_list_pages(“title_li=&child_of=”.$thispage->ID); ?>

    <? } ?>
    </div><!–subnav–>

Viewing 2 replies - 1 through 2 (of 2 total)
  • not sure if you thought of this… but would it be too messy to double check if the parent has a parent?

    as you look for post_parent!=0 for the page, you could nest another ‘if’ check to see if the parent has 0 parents… (and is hence the grandparent you’re looking for)

    does that make sense?

    $thispage = $wp_query->post;
    $parent_name = $wpdb->get_var(“SELECT post_name FROM $wpdb->posts WHERE ID = ‘$thispage->post_parent;'”);

    if($thispage->post_parent!=0 ) {
    if($parent_name->post_parent!=0 ) {
    // check if it has grandparents… (not sure if this works)
    $grandparent_name = $wpdb->get_var(“SELECT post_name FROM $wpdb->posts WHERE ID = ‘$parent_name->post_parent;'”);
    } else {
    // then it must be a parent
    }
    // continue on with first ‘if’
    } else {
    // whatever happens when it is not a parent or a grand parent…
    }

    Not sure if this is exactly what you were trying to do, but I wrote some code that will display the top level page tree no matter how many levels down you go. Basicly it check if parent is 0, if it isn’t go up a page until parent is 0, then send that page’s ID to the wp_list_pages function.

    <?php //if current post's parent is not 0, track back up the chain until parent is zero
    //then use that as the value passed in for child_of
    $emp_parent_post = 1;
    $emp_testingpage = $post->ID;
    //print $emp_testingpage." ";
    while ($emp_parent_post <> 0){
    $emp_parent_post = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = $emp_testingpage");
    //print $emp_parent_post." ";
    if ($emp_parent_post <> 0) $emp_testingpage = $emp_parent_post;
    //print $emp_testingpage." ";
    }
    ?>
    <?php wp_list_pages('title_li=<h2>Sub-Pages</h2>&child_of='.$emp_testingpage); ?>

    So no matter how many layers deep you go, it will alway display the top level tree with all childern.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘displaying subpages’ is closed to new replies.