Making a Side Page Menu in WordPress

Last Updated on Monday, 7 March 2011 08:05am Written by spunky Monday, 7 March 2011 07:26am

For WordPress sites with nested pages a side menu is a valuable element to any design. First we determine if the current page is in fact the parent or a child and collect it’s ID. Then by calling the wp_list_pages command we can list the children and determine if a side menu is even required.

<?php
$parent_id = ($post->post_parent)?$post->post_parent:$post->ID;
$children = wp_list_pages("title_li=&child_of=".$parent_id."&echo=0");
if ($children) { ?>
	<h3><?php echo get_the_title($parent_id)?></h3>
	<ul>
		<li class="<?=($parent_id==$post->ID)?'current_page_item':''?>"><a href="<?=get_permalink($parent_id)?>">Overview</a></li>
		<?php echo $children; ?>
	</ul>
<?php } ?>

Beautifully simple.


Leave a Reply