Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Displaying Child Pages WordPress

  • SOLVED

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.


<strong>Here's the code that I'm using now:</strong>

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


Answers (1)

2010-04-18

Erez S answers:

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>


WP Answers comments:

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.


Erez S comments:

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


WP Answers comments:

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?


Erez S comments:

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


Erez S comments:

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>


WP Answers comments:

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.


WP Answers comments:

Hi. Thanks again for the code but no dice.

This is displaying every page in the site.


Erez S comments:

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?


WP Answers comments:

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.


Erez S comments:

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


Erez S comments:

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