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

List top-level page's children ALWAYS WordPress

  • SOLVED

Hi,

I am trying to create a sub-menu on certain pages that lists the top-level parent's pages on all of it's children pages.

I have it working, but only to the second level. I need it to work on the third level as well though. Below is the code I am using:


<?php
if($post->post_parent){
$children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0');
} else{
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
}
if ($children) { ?>

<h2><?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
?></h2>

<ul>
<?php echo $children; ?>
</ul>

<?php }
?>


Here are the pages I am currently working on:
- [[LINK href="http://1ina100000.com/businesses-services/"]][Parent][[/LINK]]
-- [[LINK href="http://1ina100000.com/businesses-services/armor-solutions/"]][Child][[/LINK]]
--- [[LINK href="http://1ina100000.com/businesses-services/armor-solutions/vehicular-armor/"]][Grandchild][[/LINK]] - Sub-menu displaying the wrong children. Should be displaying same menu as parent and child.

Answers (3)

2010-08-07

Valentinas Bakaitis answers:

You need to see if the $post->post_parent has childrens. The code below should do the trick.

<?php
if($post->post_parent){
//get the parrent and see if it'a a top page (has no parent)
$parent = get_page($post->post_parent);
if($parent->post_parent){ //if it's not a top page, then his parent should be
$children = wp_list_pages('title_li=&child_of='.$parent->post_parent.'&echo=0');
}else{
$children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0');
}
} else{
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');\
}

if ($children) { ?>
<h2><?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
?></h2>

<ul>
<?php echo $children; ?>
</ul>

<?php }
?>


Sawyer comments:

That did the trick!

One last thing, it displays the correct pages, but on the grandchild page, the header (parent_title) changes to the child page's title. Is there anyway to make that title always stay as the top-level page's title?

Thanks!


Valentinas Bakaitis comments:

Sure. The code below should work as you expect. Though it's a bit clumsy. Give me a minute and I'll make it much better :)

<?php
if($post->post_parent){
//get the parrent and see if it'a a top page (has no parent)
$parent = get_page($post->post_parent);
if($parent->post_parent){ //if it's not a top page, then his parent should be
$children = wp_list_pages('title_li=&child_of='.$parent->post_parent.'&echo=0');
}else{
$children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0');
}
} else{
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
}

if ($children) { ?>

<h2><?php
if($parent->post_parent){
$parent_title = get_the_title($parent->post_parent);
}else{
$parent_title = get_the_title($post->post_parent);
}
echo $parent_title;
?></h2>

<ul>
<?php echo $children; ?>
</ul>

<?php } ?>


Valentinas Bakaitis comments:

Ok, this is it:

<?php
if($post->post_parent){
$parent = get_page($post->post_parent);
$first_level=($parent->post_parent)?$parent->post_parent:$post->post_parent;
}else{
$first_level = $post->ID;
}
if(wp_list_pages("title_li=&child_of=$first_level&echo=0" )){
<h2><?php echo get_the_title($first_level); ?></h2>
<ul><?php wp_list_pages("title_li=&child_of=$first_level" ); ?></ul>
<?php } ?>

2010-08-07

Tobias Bergius answers:

Try this:

<?php
if (!$post->post_parent) {
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
} else {
$p = end(get_post_ancestors($post->ID));
$children = wp_list_pages('title_li=&child_of='.$p.'&echo=0');
}
?>

<?php
if ($children) {
$parent_title = end(get_post_ancestors($post));
?>
<h2><?php echo get_the_title($parent_title); ?></h2>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>

2010-08-07

Pippin Williamson answers:

Try this:


<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
} else {
$parent = $wp_query->post->post_parent;
} ?>
<?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
<?php wp_list_pages("title_li=&child_of=$parent" ); ?>
<?php endif; ?>


Pippin Williamson comments:

This link to the WordPress function reference should help you further as well.

http://codex.wordpress.org/Function_Reference/wp_list_pages#List_subpages_even_if_on_a_subpage


Sawyer comments:

Still doing the same thing on Grandchildren pages


Sawyer comments:

I've also tried

?php
if(!$post->post_parent){
// will display the subpages of this top level page
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}else{
// diplays only the subpages of parent level
//$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");

if($post->ancestors)
{
$ancestors = end($post->ancestors);
$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
}

if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>


But it will only display children on the parent pages. What is weird about this, is the above code worked on my development site, then I moved things over to a media temple sever and it stopped working.