$8
List top-level page's children ALWAYS
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:
- [Parent]
-- [Child]
--- [Grandchild] - Sub-menu displaying the wrong children. Should be displaying same menu as parent and child.
Sawyer | 08/07/10 at 12:12pm
| Edit
(3) Possible Answers Submitted...
-

Last edited:
08/07/10
12:23pmPippin Williamson says: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; ?>
- 08/07/10 12:25pm
Pippin Williamson says: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 - 08/07/10 12:26pm
Sawyer says:Still doing the same thing on Grandchildren pages
- 08/07/10 12:30pm
Sawyer says: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.
- 08/07/10 12:25pm
-

Last edited:
08/07/10
12:53pmValentinas Bakaitis says: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 }
?>Previous versions of this answer: 08/07/10 at 12:31pm
- 08/07/10 12:33pm
Sawyer says: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! - 08/07/10 12:51pm
Valentinas Bakaitis says: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 } ?> - 08/07/10 12:59pm
Valentinas Bakaitis says: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 } ?>
- 08/07/10 12:33pm
-

Last edited:
08/07/10
12:53pmTobias Bergius says: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 } ?>
This question has expired.
Current status of this question: Completed





