WordPress has support for displaying tag cloud using wp_tag_cloud. It displays tag cloud and has support for including and excluding tags and specifying mininum and maximum number of posts.
At times we may need extra customisation (e.g. anchor title attribute, etc) in tag cloud. Here is the wordpress php code snippet you can use to have your own custom tag cloud:
<?php function my_tags() { $tags_include = array('slug1', 'slug2'); $tags_exclude = array('slug3'); $tag_count_threshold = 8; $tags = get_tags(); $html = "<div>"; $sep = ""; foreach ( $tags as $tag ) { $tag_link = get_tag_link( $tag->term_id ); $name = $tag->name; $desc = $tag->description; $count = $tag->count; if (!in_array($tag->slug, $tags_include)) { if ($count < $tag_count_threshold || in_array($tag->term_id, $tags_exclude)) { continue; } } $title = ($desc)? $desc : $name; $atext = ($desc)? $desc : $name; $html .= $sep; $sep = " | "; $html .= "<a href='{$tag_link}' title='$title' class='{$tag->slug}'>$atext</a>"; } $html .= "</div>\n"; return $html; } echo my_tags(); ?>
Approach taken in wordpress custom tag cloud code
- Retrieve all tags from db.
- If a tag slug is in include list it must be displayed.
- If a tag slug is in exclude list or has count < threshold value, hide it.
- Display all other tags.