public
Last active

Controlled through the Widget menus. It adds sub-page navigation automatically

  • Download Gist
sub-page-navigation-widget.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
<?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");'));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.