Custom Taxonomy Widget

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.

In the 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);

In the 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.


4 thoughts on “Custom Taxonomy Widget”

    1. Hello,

      Thank you for the post and the reply, it helped me a lot!
      I used the most recent version (21165.5) of the code in the ticket. There seems to be a bug with dropdown menus. I might have done something wrong, since I’m new to wordpress, but here is what I noticed:
      If “dropdown list” is left unchecked and the categories are displayed as a regular list, the links follow your Permalink settings. In my case, it follows the /%category%/%postname%/ structure.
      When I check the dropdown option, all my links in the dropdown direct to a 404 page.
      I think this is caused by the fact that the the javascript doesn’t take your permalink settings into account.
      In my case, I figured a way to present my categories nicely in a list format. Nevertheless, if it is going to be a patch in WP, I thought this bug needed to be reported.

      Thanks again!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s