0

This code displays the parent page and a list of its subpages, but not the grandparent page. (It is the second red menu in the left sidebar here).

There is one problem with this. This code outputs a list of all pages on the website if the user is on a post or archives page (see here).

How do I prevent this from happening? This code is big and clunky, is there an easier solution? Thanks for the help.

<?php //parent variables
    $parent = get_post($post->post_parent);
    $parent_title = get_the_title($parent);
    $grandparent = $parent->post_parent;
    $grandparent_title = get_the_title($grandparent);
    $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );?>

<?php // is the homepage the granparent? = third level page
if ($grandparent == is_page('0')) {
    $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page
    $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
    if ($children) { ?>
    <ul class="submenu">
      <?php echo $children; /*print list of pages*/ ?>
    </ul>
    <?php }?>

<?php // is the homepage the parent? = second level page
    } elseif ($post->post_parent ==is_page('0')) {
      $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
  if ($children) { ?>

    <ul>
      <li class="current_page_item"><?php echo get_the_title(); ?></li>
      <?php echo $children; ?>
    </ul>
    <?php
    } else {// your else stuff

    }
?>
0

If you want this for pages only, which would makes sense as to what you described this, just wrap the whole bunch of code in if ( is_page() ) { ... }.

If you were to put this into a function, you could just bail out early.

function wpdev_156446_list_parent_page_tree() {

    if ( ! is_page() ) {
        return;
    }

    // Your above code here
}

// EDIT

function wpdev_156446_list_parent_page_tree() {

    if ( ! is_page() ) {
        return;
    }

    $parent = get_post($post->post_parent);
    $parent_title = get_the_title($parent);
    $grandparent = $parent->post_parent;
    $grandparent_title = get_the_title($grandparent);
    $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );

    // is the homepage the granparent? = third level page
    if ($grandparent == is_page('0')) {
        $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page
        $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
        if ($children) {
            ?>
            <ul class="submenu">
              <?php echo $children; /*print list of pages*/ ?>
            </ul>
        <?php
        }

    // is the homepage the parent? = second level page
    } elseif ($post->post_parent ==is_page('0')) {
        $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
        if ($children) {
            ?>
            <ul>
              <li class="current_page_item"><?php echo get_the_title(); ?></li>
              <?php echo $children; ?>
            </ul>
        <?php
        } else {// your else stuff

        }
    }
}
  • When I do this I receive errors. Perhaps I am doing this incorrectly. I have inserted my code where that code says to... – AndrettiMilas Jul 31 '14 at 21:57
  • I'm sorry, I forgot the negation ! in the function. I updated that. Also, please include any errors/warnings/notices to your questions to help us all sort this out. – tfrommen Jul 31 '14 at 22:00
  • This is how I did it: snippi.com/s/o8vu44e I'm probably way off. Parse error: syntax error, unexpected $end in on line 48 – AndrettiMilas Jul 31 '14 at 22:04
  • Thank you, but now it's not appearing on pages either. :-/ – AndrettiMilas Jul 31 '14 at 22:19
  • I'm pretty sure there either is something else going on, or you are not looking at a page but some other template. – tfrommen Jul 31 '14 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.