Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Testing Site: http://www.indysouthportdental.com.php5-1.ord1-1.websitetestlink.com

I only want to display the children of parent in the sidebar when viewing any pages within the parent.

For example, Oral Health has 10 child pages. Some of those child (Treatment for example) pages have child pages (grandchildren of Oral Health).

Even if I'm on a grandchild page, I still want to list the only the child pages of the parent.

This is the code I'm using

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

This code diplays the related grandchild pages when on any grandchild page rather than the child pages.

(hope that all makes sense...)

share|improve this question
add comment

2 Answers

Got it working. This is the final code (pulled from http://cssglobe.com/post/5812/wordpress-find-pages-top-level-parent-id with little modification)

<?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&depth=1");

if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
share|improve this answer
add comment

Just use this:

<?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) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
<?php } ?>

and you should be OK

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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