HowTo – WordPress Displaying A Menu Of Child Pages On Your Sidebar

Filed under: Wordpress Tips

When you are building sites for businesses you will often find that it is better to store your content in Pages rather then in Posts.

The use of pages for the main content allows you to build a site of about 10 to 30 pages that describes your clients offerings and it can easily be displayed on your main navigation bar with drop downs.

One nice thing to do is allow your visitor to see a sidebar menu of child pages so they can select other relative topics when they are browsing. This reminds the visitor of what was in the menu that they just used to get to the page they are on…

In other words.

You have a site for a contractor. They want their main horizontal menu of pages to have parents that say

Construction  | Landscaping | Appliance Repair | Contact

Under landscaping they list Lawn Care, Tree Service, Mulching, Snow Removal

so when your visitor is browsing Lawn Care you can display a sidebar menu that lists the subpages reminding someone that is looking for Lawn Care that the company also does Mulching and it can increase sales… well you hope it does.

The Following code can be placed in your sidebar to display a sidebar menu of pages.

It is designed to only show the menu if the parent has child pages and will not show if you are in a Posts Category or Single.

I would suggest that you use a div to wrap the code to allow for css coding of your menu properties.

[php]
<pre title=""><?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 } } ?></pre>
[/php]