0

I'm wondering how to write a list of sub-pages of actually visited page.

So I have 2 pages and 3 sub-pages for each:

Colors [page]
- Red [child of Colors - subpage]
- Blue [child of Colors - subpage]
- Green [child of Colors - subpage]

Numbers [page]
- One [child of Numbers - subpage]
- Two [child of Numbers - subpage]
- Three [child of Numbers - subpage]

And when user visits "Colors" page then my code outputs Red/Blue/Green and if he does vist Numbers it shows One/Two/Three.

I'm sure wp_list_pages will do the thing easy, but I'm not sure about parameters.

2 Answers 2

2

easy just pass it the $id off which to get the children

  global $id;
  wp_list_pages("title_li=&child_of=$id");

of if you want in the loop then

 wp_list_pages("title_li=&child_of=$post->ID");
1
  • it doesn't work for me. The first code shows nothing. The second shows the list of all pages & sub-pages. Are you sure this should work inside of a widget? BTW when I type "echo $id" it echoes ID of the SIDEBAR. Mar 4, 2011 at 23:18
0

Here's a snippet from a page template I use to list subpages:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php
    $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
    if ($children) { ?>
    <ul>
        <?php echo $children; ?>
    </ul>
<?php } ?>
<?php endwhile; endif; ?>
1
  • your code also shows list of all pages and sub-pages. Mar 4, 2011 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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