Widgets: Make the categories widget work with custom taxonomies.

Props fonglh, wonderboymusic, DrewAPicture, kucrut.
Fixes #21165.


git-svn-id: https://develop.svn.wordpress.org/trunk@34376 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor
2015-09-22 04:24:53 +00:00
parent 5dd7bec848
commit 90fb9480d1
2 changed files with 40 additions and 2 deletions

View File

@@ -22,6 +22,8 @@ class WP_Widget_Categories extends WP_Widget {
public function widget( $args, $instance ) {
static $first_dropdown = true;
$current_taxonomy = $this->_get_current_taxonomy( $instance );
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base );
@@ -34,10 +36,13 @@ class WP_Widget_Categories extends WP_Widget {
echo $args['before_title'] . $title . $args['after_title'];
}
$tax = get_taxonomy( $current_taxonomy );
$cat_args = array(
'orderby' => 'name',
'show_count' => $c,
'hierarchical' => $h
'hierarchical' => $h,
'taxonomy' => $current_taxonomy,
);
if ( $d ) {
@@ -46,7 +51,7 @@ class WP_Widget_Categories extends WP_Widget {
echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
$cat_args['show_option_none'] = __( 'Select Category' );
$cat_args['show_option_none'] = $tax->labels->select_name;
$cat_args['id'] = $dropdown_id;
/**
@@ -109,6 +114,7 @@ class WP_Widget_Categories extends WP_Widget {
$instance['count'] = !empty($new_instance['count']) ? 1 : 0;
$instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
$instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] );
return $instance;
}
@@ -123,10 +129,25 @@ class WP_Widget_Categories extends WP_Widget {
$count = isset($instance['count']) ? (bool) $instance['count'] :false;
$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
$dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
$current_taxonomy = $this->_get_current_taxonomy( $instance );
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'taxonomy' ) ); ?>"><?php _e( 'Taxonomy:' ) ?></label>
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'taxonomy' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'taxonomy' ) ); ?>">
<?php foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) : ?>
<?php
if ( ! $taxonomy->hierarchical || empty( $taxonomy->labels->name ) ) :
continue;
endif;
?>
<option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php selected( $taxonomy->name, $current_taxonomy ) ?>><?php echo esc_html( $taxonomy->labels->name ); ?></option>
<?php endforeach; ?>
</select>
</p>
<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
<label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
@@ -138,4 +159,19 @@ class WP_Widget_Categories extends WP_Widget {
<?php
}
/**
* Retrieves the taxonomy for the current widget instance.
*
* @since 4.4.0
* @access public
*
* @param array $instance Current settings.
* @return string Name of the current taxonomy if set, otherwise 'category'.
*/
public function _get_current_taxonomy( $instance ) {
if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) {
return $instance['taxonomy'];
}
return 'category';
}
}