Two level-deep WordPress sidebar pages

by jon on October 11, 2008

I gave up trying to find a sidebar plugin in WordPress that would let me display sub-pages to the current page, so I ended up writing one very quickly. In the case of the code here, "Gallery" is a top-level page that is holding two levels of sub-pages for a website I'm putting together. The functions should be placed in the themes' functions.php file.

NOTE: this is by no means perfect, its just a hack that I whipped up in an hour that I figured should have been doable without hacking WordPress. I woudn't suggest writing any kind of lower-level code like this, but seeing as WordPress uses SQL calls, at least keeping them in their respective functions limits the exposure to the view.

 
function parentParentIsGallery($parentId  , $galleryPageId)
{
	global $wpdb;
	$pr = $wpdb->get_row("SELECT ID, post_title, post_status,
post_parent FROM $wpdb->posts WHERE ID = ".$parentId."");
	return (intval($pr->post_parent) == intval($galleryPageId));
 
}
function getSyblings($id)
{
	global $wpdb;
	return $wpdb->get_results("SELECT ID, post_title, post_status,
post_parent FROM $wpdb->posts WHERE post_parent = $id AND post_status='publish'");
}
 
function myMenu($galleryPageId , $postId)
{
	global $post;
 
		foreach( getSyblings($galleryPageId) as $ps )
		{
			if ($post->ID == $ps->ID)
				$class = "current_page_item";
			else
				$class= "";
			print "
<li class='$class' ><a href='".get_permalink($ps->ID)."'>$ps->post_title</a></li>
 
\n";
 
			if ($ps->ID == $postId)
			{
				print "
<ul class='subpage'>";
				foreach( getSyblings($postId) as $child )
				{
 
				if ($post->ID == $child->ID)
					$class = "current_subpage_item";
				else
					$class= "";				
 
					print "
<li class='$class'><a href='".get_permalink($child->ID)."'>$child->post_title</a></li>
 
";
 
				}
				print "</ul>
 
";
			}
		}
}
 

And here's the relevant part of sidebar.php:

 
<?php
 
$galleryPageId = 5;
?>
 
<?php if ($post->post_parent == $galleryPageId || $post->ID == $galleryPageId): ?>
<li class="widget">
<h2><?php echo $parent_title; ?> <?php _e(''); ?></h2>
<ul class="list-page">
	<?php myMenu($galleryPageId , $post->ID); ?>
                      </ul>
</li>
 
<?php elseif(parentParentIsGallery($post->post_parent , $galleryPageId)): ?>
<li class="widget">
<h2><?php echo $parent_title; ?> <?php _e(''); ?></h2>
<ul class="list-page">
	<?php myMenu($galleryPageId , $post->post_parent); ?>
                      </ul>
</li>
 
<? endif;?>
 

{ 5 comments… read them below or add one }

Ramin October 11, 2008 at 7:08 am

If you were to upgrade Wordpress after that, you would have to add this code to the functions.php file again.

Any way you could make a plugin out of it?

Sholeh October 11, 2008 at 3:54 pm

It seems as though your post has bled over to the right side of the sidebar…

jon October 11, 2008 at 8:43 pm

@Ramin:

The functions.php file belongs with the template, so upgrading WordPress shouldn’t be too much of a problem. Making a plugin out this would be ideal, but I haven’t read the docs on that yet. It’s definately on my TODO list.

@Sholeh: thanks :) Formatting fixed.

vieri December 12, 2008 at 10:18 pm

hello,

I have tried to put the code inside fuction.php and sidebar.php, but nothing change with my side bar, could you help me? for your information I use wordpress 2.6.5 and default theme.

jon December 15, 2008 at 11:10 am

make sure the file is functions.php. Also, in order for the sidebar to appear, you have to use < ?php get_sidebar(); ?> in your master template. This will include all that’s inside the sidebar.php file.

Leave a Comment

Previous post:

Next post: