Created
October 16, 2012 01:57
-
-
Save leoken/3896862 to your computer and use it in GitHub Desktop.
Subnav widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Subnav widget | |
* | |
* Base CSS: | |
* | |
* .widget_roots_subpages ul { | |
* margin-top: 10px; | |
* padding: 0; | |
* background-color: #fff; | |
* -webkit-border-radius: 6px; | |
* border-radius: 6px; | |
* -webkit-box-shadow: 0 1px 4px rgba(0,0,0,.065); | |
* -moz-box-shadow: 0 1px 4px rgba(0,0,0,.065); | |
* box-shadow: 0 1px 4px rgba(0,0,0,.065); | |
* } | |
* .widget_roots_subpages ul > li > a { margin: 0 0 -1px; font-size: 14px; padding: 9px 14px; border: 1px solid #e5e5e5; } | |
* .widget_roots_subpages ul > li:first-child > a { -webkit-border-radius: 6px 6px 0 0; border-radius: 6px 6px 0 0; } | |
* .widget_roots_subpages ul > li:last-child > a { -webkit-border-radius: 0 0 6px 6px; border-radius: 0 0 6px 6px; } | |
* .widget_roots_subpages ul .icon-chevron-right { float: right; margin-right: -6px; opacity: .25; } | |
* .widget_roots_subpages ul > li > a:hover { background-color: #f5f5f5; } | |
* .widget_roots_subpages ul a:hover .icon-chevron-right { opacity: .5; } | |
* .widget_roots_subpages ul .active .icon-chevron-right { opacity: .75; } | |
* .widget_roots_subpages ul ul { display: none; } | |
*/ | |
class Roots_Subpages_Widget extends WP_Widget { | |
function Roots_Subpages_Widget() { | |
$widget_ops = array('classname' => 'widget_roots_subpages', 'description' => __('Use this widget to add a navigation list for subpages', 'roots')); | |
$this->WP_Widget('widget_roots_subpages', __('Subpages', 'roots'), $widget_ops); | |
$this->alt_option_name = 'widget_roots_subpages'; | |
add_action('save_post', array(&$this, 'flush_widget_cache')); | |
add_action('deleted_post', array(&$this, 'flush_widget_cache')); | |
add_action('switch_theme', array(&$this, 'flush_widget_cache')); | |
} | |
function widget($args, $instance) { | |
$cache = wp_cache_get('widget_roots_subpages', 'widget'); | |
if (!is_array($cache)) { | |
$cache = array(); | |
} | |
if (!isset($args['widget_id'])) { | |
$args['widget_id'] = null; | |
} | |
if (isset($cache[$args['widget_id']])) { | |
echo $cache[$args['widget_id']]; | |
return; | |
} | |
ob_start(); | |
extract($args, EXTR_SKIP); | |
$title = apply_filters('widget_title', empty($instance['title']) ? __('Subpages', 'roots') : $instance['title'], $instance, $this->id_base); | |
echo $before_widget; | |
if ($title) { | |
echo $before_title; | |
echo $title; | |
echo $after_title; | |
} | |
global $post; | |
if (!$post->post_parent) { | |
$child_of = $post->ID; | |
} elseif ($post->ancestors) { | |
$ancestors = end($post->ancestors); | |
$child_of = $ancestors; | |
} | |
$subpages_args = array( | |
'title_li' => '', | |
'child_of' => $child_of, | |
'echo' => 0 | |
); | |
$subpages = wp_list_pages($subpages_args); | |
$subpages = str_replace('</a>', ' <i class="icon-chevron-right"></i></a>', $subpages); | |
if ($subpages) { ?> | |
<ul class="nav nav-list"> | |
<?php echo $subpages; ?> | |
</ul> | |
<?php } | |
echo $after_widget; | |
$cache[$args['widget_id']] = ob_get_flush(); | |
wp_cache_set('widget_roots_subpages', $cache, 'widget'); | |
} | |
function update($new_instance, $old_instance) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags($new_instance['title']); | |
$this->flush_widget_cache(); | |
$alloptions = wp_cache_get('alloptions', 'options'); | |
if (isset($alloptions['widget_roots_subpages'])) { | |
delete_option('widget_roots_subpages'); | |
} | |
return $instance; | |
} | |
function flush_widget_cache() { | |
wp_cache_delete('widget_roots_subpages', 'widget'); | |
} | |
function form($instance) { | |
$title = isset($instance['title']) ? esc_attr($instance['title']) : ''; | |
?> | |
<p> | |
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php _e('Title (optional):', 'roots'); ?></label> | |
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>" /> | |
</p> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment