The default Category widget lists categories and then shows all posts from a category when clicked on. What if I want something similar for custom taxonomies?
Nothing seems to exist for that yet. However, the
wp_list_categories() function has been updated to accept a taxonomy name as an argument. That’s the main function which does the magic in the default widget, the only problem now is how to pass the desired taxonomy name into the function.
Once again, copy and paste comes to the rescue. I just needed to modify a relatively small part of the Category widget, so the easiest way is to copy and paste the whole code over then change the name, similar to what I did for custom post type archives.
widget() function, the variables to control the widget are specified. I added a new one to take the taxonomy name and added it to the category arguments array.
$tax = $instance['taxonomy']; //some other code $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h,'taxonomy' => $tax);
update() function, add in code to update the taxonomy value. Since it will get input from a textbox, just like the title, the code is very similar.
$instance['taxonomy'] = strip_tags($new_instance['taxonomy']);
Copy whatever the Title textbox does in the
form() function as well.
$tax = esc_attr( $instance['taxonomy']); //html code to display the textbox, copy from the title textbox and modify name and values
Once again, remember to register this widget.
add_action('widgets_init', create_function('', 'return register_widget("WP_Widget_Custom_Taxonomies");'));
Now when you want to display terms from a custom taxonomy, just activate this widget and type in the registered name of the custom taxonomy. For better usability, type in a title too.