logo

$10
Displaying Child Pages

Hi There,

I have a fairly basic question of how to display child pages on my custom page template?

The code that I have now will will in the sub pages, but the problem is when you click on one of the subpages, the rest of the nav disappears. I need to have the sub-menu stay the same for all sub pages.

For example, here is a navigation structure:


- Page A
- Page B
- Page C
- Page D
- Page D2
- Page D3
- Page E


I do not want Page A to be in the sub-nav as it will already be in the main nav. I would want Pages B through E to be visible on all sub pages.

I really appreciate your help.


Here's the code that I'm using now:

<ul>
<?php
if($post->post_parent)
$children = wp_list_pages("sort_column=menu_order&depth=2&title_li=&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_pages("sort_column=menu_order&depth=2&title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<?php echo $children; ?>
<?php } ?>
</ul>


WP Answers | 04/18/10 at 11:46am | Edit


(1) Possible Answers Submitted...

  • avatar
    Last edited:
    04/18/10
    1:51pm
    Erez S says:

    Try this:
    <ul>


    <?php
    $children = wp_list_pages("sort_column=menu_order&depth=2&title_li=&child_of=".$post->post_parent."&echo=0");
    if ($children) { ?>
    <?php echo $children; ?>
    <?php } ?>
    </ul>

    • 04/18/10 12:03pm

      WP Answers says:

      Hi,

      Thanks for the code but it is doing the same thing.

      using the same example above, When I use that code and click on item D2, it takes me to the page and the only items in the nav are D2 and D3. I'd like the nav to stay the same no matter what page your on within that section.

      Thanks a lot.

    • 04/18/10 12:05pm

      Erez S says:

      You mean you want to show all categories on all the pages on your site?

    • 04/18/10 12:10pm

      WP Answers says:

      No.

      Ok so lets pretend that this is one section of the site:


      - Page A
      - Page B
      - Page C
      - Page D
      - Page D2
      - Page D3
      - Page E


      Page A is the main page. Pages B, C, D, E are sub pages. and paged D2, D3 are sub-sub pages.

      When you are within the section of the site, lets say on Page B. and you click to another page within the section, lets say Page D2, I want the nav to stay the same the whole time. So the nav should always look like this no matter which one of the pages you are on


      - Page B
      - Page C
      - Page D
      - Page D2
      - Page D3
      - Page E



      Does this make sense?

    • 04/18/10 12:12pm

      Erez S says:

      Yep.But in the last example,did you forgot to write Page A or this is soppused to be like that?

    • 04/18/10 12:15pm

      Erez S says:

      Try it:

      <ul>
      <?php
      if($post->post_parent)
      $children = wp_list_pages("sort_column=menu_order&depth=2&title_li=&echo=0"); else
      $children = wp_list_pages("sort_column=menu_order&depth=2&title_li=&echo=0");
      if ($children) { ?>
      <?php echo $children; ?>
      <?php } ?>
      </ul>

    • 04/18/10 12:15pm

      WP Answers says:

      No, I don't want page A to show up since it will already be in main navigation. This is for a sub navigation that will be on the left side of the page.

      Similar to the navigation on this site:
      http://www.hersheyresearch.com/park-advantage-penn-state.html

      I'm looking to create the vertical nav on the left.

    • 04/18/10 12:17pm

      WP Answers says:

      Hi. Thanks again for the code but no dice.

      This is displaying every page in the site.

    • 04/18/10 12:21pm

      Erez S says:

      Tell me if I get it right:
      You want to show every page on your site which is in the same section with the page you are in,let's assume:

      1
      2
      3
      31
      32
      33
      4
      5
      51
      52
      53
      531
      532

      On pages 1-5 you'll all pages,on page 51 you'll 51-53 plus 531-532 because they are in the same section
      Right?

    • 04/18/10 12:28pm

      WP Answers says:

      Yes correct. Sorry about the confusion ;)

      So if you were on page 51 in your example. You would see pages 51-53 as well as 531 & 532.

    • 04/18/10 12:31pm

      Erez S says:

      Great,I'll right you the code ASAP(I'm going out for about hour)
      But if you want to write it yourself,here is instructions:simply get all pages with 0 depth,and then for every page you have to check if he is parent,and if yes get his sons,but only if he is in the same depth as the page you are in
      Hope you understood,if not I'll write the code when I back

    • 04/18/10 1:46pm

      Erez S says:

      Try it:

      <?php

      if ($post->post_parent) {
      $ancestors=get_post_ancestors($post->ID);
      $root=count($ancestors)-1;
      $parent = $ancestors[$root];
      } else {
      $parent = $post->ID;
      }

      $children = wp_list_pages("title_li=&child_of=". $parent ."&echo=0");

      if ($children) { ?>
      <ul id="subnav">
      <?php echo $children; ?>
      </ul>
      <?php } ?>

This question has expired.





Current status of this question: Completed