Need A Design To WOW Your Visitors?

Open

Create a dynamic page listing widget

by

Hi Everyone! It’s been a while since my last tutorial, so here’s a quick bit of code I want to share, since I’ve found myself using it a lot.

For sites that have a large number of pages setup hierarchically, adding them all to the drop-down menu doesn’t make sense. Same with just adding the pages widget to the sidebar, since anything more than a few pages quickly becomes overwhelming. The best happy-medium I’ve found so far is to display pages that have the same parent as the current page in a list in the sidebar.

function pages_sidebar() { if(is_page()) { ?> <!-- Show only on single pages -->
  <li id="pages_widget" class="widget">
    <?php
    global $post;  // Get the post information for the current page
    if($post->post_parent)    // Check to see what the page's parent is
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&depth=1"); else    // List child pages of page's parent
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");   // List child pages of current page
    if ($children) { ?>
      <h3>Related Pages</h3>   <!-- Widget title -->
      <ul>
        <?php echo $children; ?>   <!-- Echo queried pages -->
      </ul>
    <?php } ?>
  </li>
<?php } }

Once you add that to your custom_functions.php file, you can then hook it in to your desired position. I use

add_action('thesis_hook_before_sidebar_1', 'pages_sidebar');

But any hook will do. From there, you can use CSS to style the widget however you like.

Adam is the founder and lead developer at ThesisReady.com. I’ve been designing websites and doing graphic design work since the 8th grade. I’ve spent the majority of my adult (read: post-college) life working full-time on Thesis projects and running ThesisReady.com.

Are You A Member Yet?

Become a member of ThesisReady.com for free. It only takes a second to join and you get unlimited access to tons of great content including...

  • Free Thesis Skins
  • Special Offers and Discounts
  • Early Access to New Content
  • Much, much more...
Adam Barber has written 93 posts for ThesisReady.com

Leave a Comment

Previous post:

Next post: