'widget_alpha_categories', 'description' => __( "A list or dropdown of categories" ) );
$this->WP_Widget('alpha_categories', __('Alpha Categories'), $widget_ops);
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title']);
if($instance['child_of'])
unset($instance['show_option_all']);
elseif($instance['show_option_all'])
$instance['show_option_all']=__('All',true);
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
if ($instance['dropdown']) {
wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $instance));
?>
'', 'orderby' => 'name',
'order' => 'ASC', 'show_last_update' => 0,
'style' => 'list', 'show_count' => 0,
'hide_empty' => 1, 'use_desc_for_title' => 1,
'child_of' => 0, 'feed' => '', 'feed_type' => '',
'feed_image' => '', 'exclude' => '', 'exclude_tree' => '', 'current_category' => 0,
'hierarchical' => true, 'title' => '',
'depth' => 0,'dropdown'=>0
);
$instance = wp_parse_args( (array) $instance, $defaults );
extract($instance);
$title = esc_attr( $title );
?>
/>
/>
/>
/>
/>
/>
Check wp_list_categories for help with these parameters.
'', 'orderby' => 'name',
'order' => 'ASC', 'show_last_update' => 0,
'style' => 'list', 'show_count' => 0,
'hide_empty' => 1, 'use_desc_for_title' => 1,
'child_of' => 0, 'feed' => '', 'feed_type' => '',
'feed_image' => '', 'exclude' => '', 'exclude_tree' => '', 'current_category' => 0,
'hierarchical' => true, 'title_li' => __( 'Categories' ),
'echo' => 1, 'depth' => 0
);
$r = wp_parse_args( $args, $defaults );
if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
$r['pad_counts'] = true;
}
if ( isset( $r['show_date'] ) ) {
$r['include_last_update_time'] = $r['show_date'];
}
if ( true == $r['hierarchical'] ) {
$r['exclude_tree'] = $r['exclude'];
$r['exclude'] = '';
}
extract( $r );
$categories = get_alpha_categories( $r );
$output = '';
if ( $title_li && 'list' == $style )
$output = '' . $r['title_li'] . '';
if ( empty( $categories ) ) {
if ( 'list' == $style )
$output .= '- ' . __( "No categories" ) . '
';
else
$output .= __( "No categories" );
} else {
global $wp_query;
if( !empty( $show_option_all ) )
if ( 'list' == $style )
$output .= '- ' . $show_option_all . '
';
else
$output .= '' . $show_option_all . '';
if ( empty( $r['current_category'] ) && is_category() )
$r['current_category'] = $wp_query->get_queried_object_id();
if ( $hierarchical )
$depth = $r['depth'];
else
$depth = -1; // Flat.
$output .= walk_category_tree( $categories, $depth, $r );
}
if ( $title_li && 'list' == $style )
$output .= '
';
$output = apply_filters( 'wp_list_categories', $output );
if ( $echo )
echo $output;
else
return $output;
}
function get_alpha_categories( $args = '' ) {
$defaults = array('orderby' => 'name', 'order' => 'ASC',
'hide_empty' => true, 'exclude' => '', 'exclude_tree' => '', 'include' => '',
'child_of' => 0, 'get' => ''
);
$args = wp_parse_args( $args, $defaults );
$categories=get_categories('pad_counts=1&hide_empty=0');
extract($args);
if($include){
$GLOBALS['alpha_include']=explode(',', $include);
$categories=array_filter($categories,'alpha_include_category');
}else{
if($child_of){
$GLOBALS['alpha_child_of']=$child_of;
$categories=array_filter($categories,'alpha_child_of_category');
}
if($exclude){
$GLOBALS['alpha_exclude']=explode(',', $exclude);
$categories=array_filter($categories,'alpha_exclude_category');
}
if($hide_empty){
$categories=array_filter($categories,'alpha_hide_empty_category');
}
}
if($orderby!='name'){
if($orderby=='count')
$orderby='category_count';
elseif($orderby=='id')
$orderby='term_id';
$categories=alpha_flat_sort_categories($categories,$orderby);
}
if(strtolower($order) !='asc'){
$categories=array_reverse($categories);
}
foreach ( array_keys( $categories ) as $k )
_make_cat_compat( $categories[$k] );
return $categories;
}
function alpha_include_category($category) {
return in_array($category->term_id, $GLOBALS['alpha_include']);
}
function alpha_child_of_category($category) {
return $category->category_parent==$GLOBALS['alpha_child_of'];
}
function alpha_exclude_category($category) {
return !in_array($category->term_id, $GLOBALS['alpha_exclude']);
}
function alpha_hide_empty_category($category) {
return $category->category_count>0;
}
function alpha_flat_sort_categories($categories,$orderby) {
foreach($categories as $cat){
$key=$cat->$orderby;
if($orderby=='category_count'){
$key.='-'.$cat->term_id;
}
$new[$key]=$cat;
}
ksort($new);
return array_values($new);
}
?>