|  | <?php | 
        
          |  |  | 
        
          |  | /* | 
        
          |  | Sub-Page Navigation Widget | 
        
          |  | Controlled through the Widget menus. | 
        
          |  | It adds sub-page navigation automatically. | 
        
          |  | It has no options. | 
        
          |  | */ | 
        
          |  |  | 
        
          |  | class SubPage_Navigation_Widget extends WP_Widget { | 
        
          |  |  | 
        
          |  | // Constructor | 
        
          |  | function SubPage_Navigation_Widget() { | 
        
          |  | $sw_opts = array( | 
        
          |  | 'classname' => 'subpage_navigation_widget', | 
        
          |  | 'description' => 'Sub-navigation for pages that have child pages' | 
        
          |  | ); | 
        
          |  | $control_opts = array( 'id_base' => 'subpage_navigation_widget' ); | 
        
          |  | $this->WP_Widget('subpage_navigation_widget', 'Page Sub-Navigation', $sw_opts, $control_opts ); | 
        
          |  | } | 
        
          |  |  | 
        
          |  |  | 
        
          |  | // Outputting our Widget | 
        
          |  | function widget($args, $instance) { | 
        
          |  | extract( $args ); | 
        
          |  |  | 
        
          |  | // Set up our initial variables. | 
        
          |  | global $post; | 
        
          |  | $nav = ''; | 
        
          |  |  | 
        
          |  | // If we're not on a page that has a post (empty serch page?), let's just stop. | 
        
          |  | if ( empty($post) ) | 
        
          |  | return; | 
        
          |  | if ( is_category() || is_tag() || is_archive() || is_search() || is_home() || is_single() ) | 
        
          |  | { | 
        
          |  | $title = 'Categories:'; | 
        
          |  |  | 
        
          |  | // Create a list of categories | 
        
          |  | $args = array( | 
        
          |  | 'title_li' => '', | 
        
          |  | 'echo' => 0 | 
        
          |  | ); | 
        
          |  | $nav = wp_list_categories( $args ); | 
        
          |  |  | 
        
          |  | } else { | 
        
          |  | // Where are we? | 
        
          |  | $parent_id = $this->get_parent_id($post); | 
        
          |  | $title = get_the_title($parent_id); | 
        
          |  | $link =  get_permalink($parent_id); | 
        
          |  | $title = "<a href='$link' title='$title'>$title:</a>"; | 
        
          |  |  | 
        
          |  | if (  !empty($parent_id)  ) { | 
        
          |  | $args = array( | 
        
          |  | 'title_li' => '', | 
        
          |  | 'child_of' => $parent_id, | 
        
          |  | 'echo' => 0, | 
        
          |  | 'sort_column' => 'menu_order' | 
        
          |  | ); | 
        
          |  |  | 
        
          |  | $nav = wp_list_pages($args); | 
        
          |  | } | 
        
          |  | } | 
        
          |  |  | 
        
          |  | // Let's hide this if there aren't any sub-pages or sibling pages... it tends to break. | 
        
          |  | if ( !empty($nav) ) { | 
        
          |  | // Before widget // | 
        
          |  | echo $before_widget; | 
        
          |  |  | 
        
          |  | // Output widget // | 
        
          |  | if ( !empty($title) ) { echo $before_title . $title . $after_title; } | 
        
          |  | echo "\n\t\t\t<ul id=\"sidebar-subnav\">\n"; | 
        
          |  | echo $nav; | 
        
          |  | echo "\n\t\t\t</ul>"; | 
        
          |  |  | 
        
          |  | // After widget // | 
        
          |  | echo $after_widget; | 
        
          |  | } | 
        
          |  | } | 
        
          |  |  | 
        
          |  |  | 
        
          |  | // Update settings | 
        
          |  | function update($new_instance, $old_instance) { | 
        
          |  | $instance = $new_instance; | 
        
          |  | return $instance; | 
        
          |  | } | 
        
          |  |  | 
        
          |  |  | 
        
          |  | // Widget Control Panel // | 
        
          |  |  | 
        
          |  | function form( $instance ) { | 
        
          |  | $defaults = array( ); | 
        
          |  | $instance = wp_parse_args( (array) $instance, $defaults ); | 
        
          |  |  | 
        
          |  | // Build the widget form | 
        
          |  | ?> | 
        
          |  | <p>No options for this widget. It will automatically build a navigation list based on the sub-pages of the top-level page.</p> | 
        
          |  | <?php | 
        
          |  | } | 
        
          |  |  | 
        
          |  | // Helper to find the base ancestor of a page that's not the home page // | 
        
          |  | function get_parent_id($post) { | 
        
          |  | if ( $post->post_parent ) { | 
        
          |  | $ancestors = get_post_ancestors($post->ID); | 
        
          |  | $root = count($ancestors)-1; | 
        
          |  | $parent = get_post($ancestors[$root]); | 
        
          |  | } else { | 
        
          |  | $parent = $post; | 
        
          |  | } | 
        
          |  | return $parent->ID; | 
        
          |  | } // end get_parent_id | 
        
          |  | } | 
        
          |  | add_action('widgets_init', create_function('', 'return register_widget("SubPage_Navigation_Widget");')); | 
        
          |  |  |