You are here: Home » Blog » News » Change Title Text of WordPress Tag Cloud Links

Change Title Text of WordPress Tag Cloud Links

Default WordPress Tag Cloud

As of WordPress Version 2.3 the wp_tag_cloud() function is available to developers which will display a list of tags, where the size of each tag is determined by how many times that particular tag has been assigned. The default title text for tag cloud links has the following format ‘%s topic’, ‘%s topics’ (where %s is the number of times the tag has been assigned).

Default WordPress Tag Cloud Title
Tag cloud with default title.

Custom WordPress Tag Cloud

Thanks to the taxonomy parameter this function can also be used for custom taxonomies. Let’s say you built your custom taxonomy (i.e. sports) and would like to display a tag cloud where the title for the sports tags is athlete(s). The function parameter topic_count_text_callback will allow you to override the default title tag function in WordPress.

For this to work, you will need to define a custom function in your theme’s functions.php file and then use it with the topic_count_text_callback parameter in the wp_tag_cloud() function. Here’s the code for our sports tag function.

function sports_tag_text_callback( $count ) {
 return sprintf( _n('%s Athlete', '%s Athletes', $count), number_format_i18n( $count ) );
}

Here’s how you would use your custom function anywhere in one of your theme’s template files (i.e. sidebar.php) with the wp_tag_cloud() function.

wp_tag_cloud( array( 'taxonomy' => 'sports', 'topic_count_text_callback' => 'sports_tag_text_callback' ) );

Our sports cloud now uses athlete(s) as the title text of the tag cloud links.

Custom WordPress Tag Cloud Title
Tag cloud with custom title.

Leave a Reply