Last active
December 17, 2015 06:39
-
-
Save jayseventwo/5566772 to your computer and use it in GitHub Desktop.
Display WordPress categories - add to functions.php
This file contains 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
class DisplayCategoriesWidget extends WP_Widget | |
{ | |
function DisplayCategoriesWidget() | |
{ | |
$widget_ops = array('classname' => 'DisplayCategoriesWidget', 'description' => 'Displays categories' ); | |
$this->WP_Widget('DisplayCategoriesWidget', 'Display Categories Widget', $widget_ops); | |
} | |
function form($instance) | |
{ | |
$instance = wp_parse_args( (array) $instance, array( 'title' => '','cat_id' => '' ) ); | |
$title = $instance['title']; | |
$category = $instance['category']; | |
$dcw_limit = $instance['dcw_limit']; | |
$display_parent = $instance['display_parent']; | |
$display_empty_categories = $instance['display_empty_categories']; | |
$showcount_value = $instance['showcount_value']; | |
// Get the existing categories and build a simple select dropdown for the user. | |
$categories = get_categories(array( 'hide_empty' => 0)); | |
$cat_options = array(); | |
$cat_options[] = '<option value="BLANK">Select one...</option>'; | |
foreach ($categories as $cat) { | |
$selected = $category === $cat->cat_ID ? ' selected="selected"' : ''; | |
$cat_options[] = '<option value="' . $cat->cat_ID .'"' . $selected . '>' . $cat->name . '</option>'; | |
} | |
?> | |
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p> | |
<!--<p><label for="<?php echo $this->get_field_id('cat_id'); ?>">Category id: <input class="widefat" id="<?php echo $this->get_field_id('cat_id'); ?>" name="<?php echo $this->get_field_name('cat_id'); ?>" type="text" value="<?php echo attribute_escape($cat_id); ?>" /></label></p>--> | |
<p> | |
<label for="<?php echo $this->get_field_id('category'); ?>"> | |
<?php _e('Choose category (optional):'); ?> | |
</label> | |
<select id="<?php echo $this->get_field_id('category'); ?>" class="widefat" name="<?php echo $this->get_field_name('category'); ?>"> | |
<?php echo implode('', $cat_options); ?> | |
</select> | |
</p> | |
<p><label for="<?php echo $this->get_field_id('dcw_limit'); ?>"><?php _e('Limit (optional):'); ?> <input class="widefat" id="<?php echo $this->get_field_id('dcw_limit'); ?>" name="<?php echo $this->get_field_name('dcw_limit'); ?>" type="text" value="<?php echo attribute_escape($dcw_limit); ?>" /></label></p> | |
<p><?php _e('Display Parent? (optional):'); ?> <br><input name="<?php echo $this->get_field_name('display_parent'); ?>" type="radio" value="1" <?php if(attribute_escape($display_parent)==1) echo "checked"; ?> />Yes <input name="<?php echo $this->get_field_name('display_parent'); ?>" type="radio" value="0" <?php if(attribute_escape($display_parent)==0) echo "checked"; ?>/>No </p> | |
<br> | |
<p><?php _e('Display Empty categories? (optional):'); ?> <br><input name="<?php echo $this->get_field_name('display_empty_categories'); ?>" type="radio" value="0" <?php if(attribute_escape($display_empty_categories)==0) echo "checked"; ?> />Yes <input name="<?php echo $this->get_field_name('display_empty_categories'); ?>" type="radio" value="1" <?php if(attribute_escape($display_empty_categories)==1) echo "checked"; ?>/>No </p> | |
<p><?php _e('Display Number of posts near categories? (optional):'); ?> <br><input name="<?php echo $this->get_field_name('showcount_value'); ?>" type="radio" value="1" <?php if(attribute_escape($showcount_value)==1) echo "checked"; ?> />Yes <input name="<?php echo $this->get_field_name('showcount_value'); ?>" type="radio" value="0" <?php if(attribute_escape($showcount_value)==0) echo "checked"; ?>/>No </p> | |
<?php | |
} | |
function update($new_instance, $old_instance) | |
{ | |
$instance = $old_instance; | |
$instance['title'] = $new_instance['title']; | |
$instance['category'] = $new_instance['category']; | |
$instance['dcw_limit'] = $new_instance['dcw_limit']; | |
$instance['display_parent'] = $new_instance['display_parent']; | |
$instance['display_empty_categories'] = $new_instance['display_empty_categories']; | |
$instance['showcount_value'] = $new_instance['showcount_value']; | |
return $instance; | |
} | |
function widget($args, $instance) | |
{ | |
extract($args, EXTR_SKIP); | |
echo $before_widget; | |
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']); | |
$cat_id = $instance['category']; | |
$dcw_limit = $instance['dcw_limit']; | |
$display_empty_categories = $instance['display_empty_categories']; | |
$showcount_value = $instance['showcount_value']; | |
if (!empty($title)) | |
echo $before_title . $title . $after_title;; | |
if($instance['display_parent']==1) | |
{ | |
$yourcat= get_category($cat_id); | |
if ($yourcat) echo '<h2>' . $yourcat->name . '</h2>'; | |
} | |
echo "<ul>"; | |
wp_list_categories('orderby=name&show_count='.$showcount_value.'&child_of='.$cat_id.'&hide_empty='.$display_empty_categories.'&title_li=&number='.$dcw_limit); | |
echo "</ul>"; | |
echo $after_widget; | |
} | |
} | |
add_action( 'widgets_init', create_function('', 'return register_widget("DisplayCategoriesWidget");') ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment