Had a go at creating a simple widget that displays pages related to the current page in the sidebar. WordPress has some pretty good documentation and there are plenty of examples around, so it was pretty simple:-

<?php
/*
Plugin Name: Show Related Pages
Description: Menu widget to show child pages of the current page.
Version: 0.1
Author: Marc Symonds
Date: July 2014
*/

class mss_showchildpageswidget extends WP_Widget
{
  // Constructor.
  
  function __construct()
  {
    parent::__construct(
        'mss_showchildpageswidget',
        'Show Child Pages',
        array('description' => 'Show child pages related to the current page')
      );
  }
  
  // Output front-end markup.
  
  public function widget($args, $instance)
  {
    global $post;

    // Only output something if viewing a page.
    
    if (is_singular() && $post->post_type == 'page')
    {
      // Get the ancestor pages for the current page.
      
      $elders = get_post_ancestors($post->ID);
      $ecount = count($elders);

      // The list we are going to build will contain the two most recent ancestors (parent and grandparent),
      // the current page with two levels of descendants (child and grandchild) and the siblings of the current
      // page with one level of descendant (child):-
      //
      // Grandparent
      //   Parent
      //     This page
      //       Child1
      //       Child2
      //         Grandchild1
      //     Sibling1
      //     Sibling2
      //       Child1
      // 

      $t = get_the_title($post->ID);
      $l = get_permalink($post->ID);

      $before_child = '<ul class="children"><li><a href="' . $l . '"><b>' . $t . '</b></a>';
      $after_child = '</li></ul>';

      $i = 2;
      $j = 0;
      while ($i > 0 && $j < $ecount)
      {
        $p = $elders[$j];
        $t = get_the_title($p);
        $l = get_permalink($p);
        
        $before_child = '<ul class="children"><li><a href="' . $l . '">' . $t . '</a>' . $before_child;
        $after_child = '</li></ul>' . $after_child;
        $i--;
        $j++;
      }

      // Get list of published pages that are children of the current page.

      $opts = array(
         'child_of'    => $post->ID,
         'depth'       => 2,
         'echo'        => 0,
         'post_type'   => 'page',
         'post_status' => 'publish',
         'show_date'   => '',
         'sort_column' => 'menu_order, post_title',
         'sort_order'  => '',
         'title_li'    => '', 
         'walker'      => '');

      $children = wp_list_pages($opts);
    
      // Get list of sibling pages of the current page.
      
      if ($ecount > 0)
      {
        $opts['child_of'] = $elders[0];
        $opts['depth'] = 2;
        $opts['exclude_tree'] = $post->ID;
        
        $siblings = wp_list_pages($opts);
      }
      else
        $siblings = '';

      // Output.
            
      echo $args['before_widget'];

      $t = $instance['title'];
      $t = apply_filters('widget_title', empty($t) ? 'Related Pages' : $t);

      if (! empty($t))
        echo $args['before_title'] . $t . $args['after_title'];

      echo $before_child;
      echo '<ul class="children">' . $children . '</ul>';
     
      echo $siblings;
         
      echo $after_child;      
        
      echo $args['after_widget'];     
    }
  }
} 


// Register my widget.

function mss_showchildpageswidget_load()
{
  register_widget('mss_showchildpageswidget');
}
add_action( 'widgets_init', 'mss_showchildpageswidget_load' );
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Spam Blocker