Simple Trick: Displaying Links to other Child Pages from the Same Parent

Imagine this situation. You have a Page as parent and a few Pages as its child Pages. When you open the parent Page, you want to display links to its child Pages on your sidebar. When you open one of the child Page, though, you still want to display that same set of links. Problem is, now you have to display the other child Pages belonging to the same parent Page instead. Or in other words, you can’t solve this with a single function call.

It might sound simple, but when you start thinking about coding it in, things might get complicated. Fortunately, there’s an elegant solution on the Codex:

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

You may also like...

3 Responses

  1. July 18, 2008

    […] Via Simple Trick: Displaying Links to other Child Pages from the Same Parent […]

  2. August 7, 2008

    […] How to Display Page Children on the Parent Page – WPLover provides some code you can use to place links to your page children on the parent page.   This is a nice idea for your themes sidebar. […]

  3. August 7, 2008

    […] Links to other Child Pages from the Same Parent http://wplover.com/simple-trick-displaying-links-to-other-child-pages-from-the-same-parent/ Earning With Google […]

css.php