I've been using the code below to auto display pages and child pages in a sidebar. Our site has a few parent pages and the rest of the pages are all nested within these. Right now the code displays the parent page, child pages, and all grandchildren pages no matter what page a user is on. I would like to modify the code to display the parent page for the title and child pages ONLY in the ul. It would also be cool to be able to leave out a page if needed.
<?php
if($post->post_parent) {
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
$titlenamer = get_the_title($post->post_parent);
}
else {
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
$titlenamer = get_the_title($post->ID);
}
if ($children) { ?>
<h2> <?php echo $titlenamer ?> </h2>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
Example of Output
If a user is on any of the pages below the menu looks the same:
<h2>Athletics</h2>
- Baseball
- HS Baseball
- Basketball
- HS Basketball – Boys
- HS Basketball – Girls
- JH Basketball – Boys
- JH Basketball – Girls
- Cheerleading
- HS Cheerleading
- JH Cheerleading
- Football
- HS Football
- JH Football
- Softball
- HS Softball
- Track & Field
- HS Track & Field – Boys
- HS Track & Field – Girls
- Indoor Track
- JH Track & Field – Boys
- JH Track & Field – Girls
- Volleyball
- HS Volleyball
- JH Volleyball
Desired Output Example
If a user is on the basketball page the menu only looks like this:
<h2>Basketball</h2>
- HS Basketball – Boys
- HS Basketball – Girls
- JH Basketball – Boys
- JH Basketball – Girls
If a user is on the athletic root parent page the menu would look like this:
<h2>Athletics</h2>
- Baseball
- Basketball
- Cheerleading
- Football
- Softball
- Track & Field
- Volleyball