From 90fb9480d18bcd7112e1d3ca151d52762d0c4d05 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 22 Sep 2015 04:24:53 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy-functions.php | 2 + .../widgets/class-wp-widget-categories.php | 40 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/taxonomy-functions.php b/src/wp-includes/taxonomy-functions.php index 1ab178e368..9c5cb6c9e1 100644 --- a/src/wp-includes/taxonomy-functions.php +++ b/src/wp-includes/taxonomy-functions.php @@ -459,6 +459,7 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) { * * - name - general name for the taxonomy, usually plural. The same as and overridden by $tax->label. Default is Tags/Categories * - singular_name - name for one object of this taxonomy. Default is Tag/Category + * - select_name - prompt to select a taxonomy when using a dropdown list in the Categories widget. Default is 'Select Category'. * - search_items - Default is Search Tags/Search Categories * - popular_items - This string isn't used on hierarchical taxonomies. Default is Popular Tags * - all_items - Default is All Tags/All Categories @@ -497,6 +498,7 @@ function get_taxonomy_labels( $tax ) { $nohier_vs_hier_defaults = array( 'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ), 'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ), + 'select_name' => array( null, __( 'Select Category' ) ), 'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ), 'popular_items' => array( __( 'Popular Tags' ), null ), 'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ), diff --git a/src/wp-includes/widgets/class-wp-widget-categories.php b/src/wp-includes/widgets/class-wp-widget-categories.php index 313041a332..dabfc0ee6b 100644 --- a/src/wp-includes/widgets/class-wp-widget-categories.php +++ b/src/wp-includes/widgets/class-wp-widget-categories.php @@ -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 ''; - $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 ); ?>

+

+ + +

+

/>
@@ -138,4 +159,19 @@ class WP_Widget_Categories extends WP_Widget {