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.

I am trying to create a list of subpages of a parent page, which will appear in the sidebar of my wordpress site. This sidebar will appear on every page.

So for example, I have a page with an ID of 54. This page has 7 subpages. I would like to display these 7 pages in the sidebar (just the titles), as well as any more subpages that get added.

There is a currently a widget called 'Pages' that will do this, but I would like to do this via code directly in the sidebar.php rather than using a widget as there are a few constraints with using the widget.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Simplest solution would be:

<li><ul>
<?php wp_list_pages('title_li=&child_of='.$post->ID.''); ?>
</ul></li>

Use this code for more flexibility:

<?php if ( is_page() ) { ?>

<?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) { ?>

<li>
<h2>
<?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
?>
</h2>
<ul>
<?php echo $children; ?>
</ul>
</li>
<?php } } ?>
share|improve this answer
    
That worked like a treat, thank you! –  Adam Dec 8 '12 at 20:43

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.