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:

{ 15 comments… read them below or add one }

√únsal Korkmaz September 28, 2011

Thanks a lot :)

Reply

Jason Manheim September 28, 2011

My pleasure.

Reply

Deniz Porsuk February 9, 2012

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

Reply

Jason Manheim February 9, 2012

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.

Reply

Sébastien Pillien July 15, 2012

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.

Reply

Jason Manheim July 15, 2012

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

Reply

Albert August 2, 2012

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

Reply

Albert August 2, 2012

ps. don’t show, not exclude.

Reply

Jason Manheim August 2, 2012

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.

Reply

Sacha September 11, 2012

Wonderful, simple and clean.
Thanks

Reply

Hemant Aggarwal September 27, 2012

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

Reply

Wynd October 11, 2012

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?

Reply

Jason Manheim October 11, 2012

That’s what custom taxonomies (http://codex.wordpress.org/Taxonomies) and custom post types (https://codex.wordpress.org/Post_Types) are for.

Reply

Sjefx Grafx March 4, 2013

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.

Reply

Shawn Nolan Jr. April 5, 2013

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

Reply

Leave a Comment