Monday, November 22, 2010

Listing Sub-Pages on your Page in WordPress

I know I'm a WP noob, but this took me about an hour to figure out. Maybe I can save someone else some time.

Most of the code below comes from the WordPress docs, but there was one critical piece missing, $post->ID was returning nothing, even though I was in "the loop". I got around that whole thing once I learned that you can get the page id with get_the_ID().

<?php
// display only this page's children
$my_id = get_the_ID();
$subpages = wp_list_pages("title_li=&echo=0&depth=1&child_of=".$my_id);

if ($subpages) { ?>
<h2>Sub-topics</h2>
<ul>
<?php echo $subpages; ?>
</ul>         
<?php } ?>

I am using WordPress 3.0.1, if that helps anyone.

No comments: