0

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

1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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