How to alter the default widget to display a tag cloud of a custom taxonomy?
2 Answers
Don't know of any but you can easily create your own:
<?php
add_action("widgets_init", array('Widget_Custom_tax_tag_cloud', 'register'));
class Widget_Custom_tax_tag_cloud {
function control(){
echo 'No control panel';
}
function widget($args){
echo $args['before_widget'];
echo $args['before_title'] . 'Your widget title' . $args['after_title'];
$cloud_args = array('taxonomy' => 'Your taxonomy here');
wp_tag_cloud( $cloud_args );
echo $args['after_widget'];
}
function register(){
register_sidebar_widget('Widget name', array('Widget_Custom_tax_tag_cloud', 'widget'));
register_widget_control('Widget name', array('Widget_Custom_tax_tag_cloud', 'control'));
}
}
?>
just change : 'Your widget title' with your title and Your 'taxonomy here' with the name of your taxonomy.
you can change the look and feel by passing more arguments in the $cloud_args from the large list in the codex
Hope this helps.
The existing answer here is excellent, but unfortunately due to the answer's age it does not work for newer versions of WordPress.
The code below improves in two ways:
1 - It's the recommended / best practice method for newer versions of WordPress, as of version 2.8
2 - It allows you to select the taxonomy through the dashboard widget interface, rather than having it hard-coded.
add_action( 'widgets_init', 'custom_register_plugin_widget' );
function custom_register_plugin_widget() {
register_widget( 'Widget_Custom_Tax_Tag_Cloud' );
}
/**
* New "best practice" is to extend the built-in WP_Widget class
*
* Class Widget_Custom_tax_tag_cloud
*/
class Widget_Custom_Tax_Tag_Cloud extends WP_Widget {
function __construct() {
parent::__construct( 'custom_tax_tag_cloud', 'Custom Taxonomy Tag Cloud', array( 'description' => 'Display a tag cloud for a custom taxonomy.' ) );
}
/**
* Allows for manipulation, calculation, etc. when saving the widget instance in the dashboard.
*
* @param array $new_instance
* @param array $old_instance
*
* @return array
*/
function update( $new_instance, $old_instance ) {
return $new_instance;
}
/**
* Echos the widget contents in a sidebar
*
* @param array $args - the general widget arguments
* @param array $instance - the settings for this specific widget
*/
function widget( $args, $instance ) {
echo $args['before_widget'];
echo $args['before_title'] . 'Your widget title' . $args['after_title'];
$cloud_args = array( 'taxonomy' => 'catalogtag' );
wp_tag_cloud( $cloud_args );
echo $args['after_widget'];
}
/**
* Render the "Controls" in the dashboard menu under Appearance => Widgets
*
* @param array $instance - the settings for this instance of the widget
*
* @return null
*/
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'taxonomy' => 'post_tag' ) );
// Load the list of taxonomies available
$taxonomies = get_taxonomies( array( 'public' => TRUE , 'show_tagcloud' => TRUE), 'objects' );
echo '<p><label>Title</label><input name="' . $this->get_field_name( 'title' ) . '" id="' . $this->get_field_id( 'title' ) . '" value="' . esc_attr( $instance['title'] ) . '" /></p>';
echo '<p><label>Taxonomy</label><select name="' . $this->get_field_name('taxonomy') . ' id="' . $this->get_field_id('taxonomy') . '">';
echo '<option value="">Select Taxonomy...</option>';
foreach($taxonomies AS $tax) {
echo '<option value="' . $tax->name . '"';
echo ($tax->name == $instance['taxonomy']) ? ' selected' : '';
echo '>';
echo ( ! empty($tax->labels->singular_name)) ? $tax->labels->singular_name : $tax->label;
echo '</option>';
}
echo '</select></p>';
}
}
While technically you could just add this to your theme's functions file, I tend to prefer putting it into a separate theme file (such as widgets.php
, and including that file in the functions file.