How to customize the WordPress tag cloud widget by using the widget_tag_cloud_args filter. Exclude tags, change font sizes and more.

Customize the WordPress Tag Cloud Widget

Written by in Tutorials |

WordPress Tag Cloud Widget

WordPress Tag Cloud Widget

During a recent redesign for a client, I needed to alter the default WordPress tag cloud widget to exclude a certain number of tags as well as include a custom taxonomy.

There are plenty of ways of adding a custom widget, or hacking WordPress core files to achieve what I needed but that isn’t very efficient; especially when it comes time to update the WordPress install.

Instead, I took advantage of filters, specifically widget_tag_cloud_args. You can find the different parameters for the wp_tag_cloud in the Codex since that’s what we’ll be working with here.

Open up your themes functions.php file (custom_functions.php for Thesis users) and add the following:

Changing the Smallest and Largest Font Sizes

By default, the tag cloud displays tags by point (pt); the smallest being 8pt and the largest being 22pt. Let’s change that to 10px and 18px respectively:

function custom_tag_cloud_widget($args) {
	$args['largest'] = 18; //largest tag
	$args['smallest'] = 10; //smallest tag
	$args['unit'] = 'px'; //tag font unit
	return $args;
}
add_filter( 'widget_tag_cloud_args', 'custom_tag_cloud_widget' );

Changing the Total Number of Tags to Display

By default the WordPress tag cloud only shows 45 tags. We can change that to display a specific number or all tags by adding the following to the function we added above:

function custom_tag_cloud_widget($args) {
	$args['number'] = 0; //adding a 0 will display all tags
	$args['largest'] = 18; //largest tag
	$args['smallest'] = 10; //smallest tag
	$args['unit'] = 'px'; //tag font unit
	return $args;
}
add_filter( 'widget_tag_cloud_args', 'custom_tag_cloud_widget' );

Changing the Tag Cloud Format

By default the tag cloud displays tags in a flat format seperated by whitespace. Let’s change that to an unordered list with a class of wp-tag-cloud:

function custom_tag_cloud_widget($args) {
	$args['number'] = 0; //adding a 0 will display all tags
	$args['largest'] = 18; //largest tag
	$args['smallest'] = 10; //smallest tag
	$args['unit'] = 'px'; //tag font unit
	$args['format'] = 'list'; //ul with a class of wp-tag-cloud
	return $args;
}
add_filter( 'widget_tag_cloud_args', 'custom_tag_cloud_widget' );

Exclude Tags from the Tag Cloud

We can also exclude tags from showing up in the tag cloud by adding the following:

function custom_tag_cloud_widget($args) {
	$args['number'] = 0; //adding a 0 will display all tags
	$args['largest'] = 18; //largest tag
	$args['smallest'] = 10; //smallest tag
	$args['unit'] = 'px'; //tag font unit
	$args['format'] = 'list'; //ul with a class of wp-tag-cloud
	$args['exclude'] = array(20, 80, 92); //exclude tags by ID
	return $args;
}
add_filter( 'widget_tag_cloud_args', 'custom_tag_cloud_widget' );

Add Custom Taxonomies to the Tag Cloud

By default the tag cloud only shows the post_tag taxonomy. Let’s also add a custom taxonomy called ‘ingredients’:

function custom_tag_cloud_widget($args) {
	$args['number'] = 0; //adding a 0 will display all tags
	$args['largest'] = 18; //largest tag
	$args['smallest'] = 10; //smallest tag
	$args['unit'] = 'px'; //tag font unit
	$args['format'] = 'list'; //ul with a class of wp-tag-cloud
	$args['exclude'] = array(20, 80, 92); //exclude tags by ID
	$args['taxonomy'] = array('post_tag', 'ingredients'); //add post tags and ingredients taxonomy
	return $args;
}
add_filter( 'widget_tag_cloud_args', 'custom_tag_cloud_widget' );

Pretty simple, no? Let us know if you need any help in the comments below.

Enjoy this post? Consider sharing it:

  • http://www.unsalkorkmaz.com Ünsal Korkmaz

    Thanks a lot :)

    • http://designpx.com/ Jason Manheim

      My pleasure.

  • http://www.netinial.com Deniz Porsuk

    Great information, is not is easier to use wp_tag_cloud(‘smallest=8&largest=16&number=20′); with variables?

    • http://designpx.com/ Jason Manheim

      This tutorial is for altering the tag cloud widget that comes standard with all WordPress installs. The function you mentioned is for adding a tag cloud to your template files.

    • Sébastien Pillien

      Easier, may be.. it depends on your way of coding.. In my own i prefer an indexed array as variable.
      No string, not function added in filter.. an array.

      • http://designpx.com/ Jason Manheim

        Do keep in mind that these are for doing two different things…

  • Albert

    How do i exclude the tags with less than 2 posts?

    • Albert

      ps. don’t show, not exclude.

      • http://designpx.com/ Jason Manheim

        Create a function that gives you all the tag ID’s of tags with less than 2 posts and echo it out to the excludes array in the code above.

  • Sacha

    Wonderful, simple and clean.
    Thanks

  • http://geekyminds.com/ Hemant Aggarwal

    Very nice article. But please tell me how to get tag ID’s

  • Wynd

    Do you know if it is possible to have a tag cloud to display tags that appear for a particular category.

    For example if I have a number of posts for that are associated to the category called film, and for each post I have several tags for the film genres (some films could have several genres i.e. Comedy & Horror). So would it be possible to show a tag cloud for only the the Film Category, that excludes all the other tags and categories that I have elsewhere on my site?

  • http://www.facebook.com/sjefx Sjefx Grafx

    Thank you so much for these instructions. I needed some adjustments but didn’t want to change WP or Theme corefiles or step to a plugin. Cut and paste your functions in childtheme functions.php saved my day.

  • http://shawnnolan.com Shawn Nolan Jr.

    what about the $args['separator'] = ‘ – ‘;
    I’m looking into making it do something like [tagname] [anothertagname]
    any ideas?