Tag clouds are a Thing. I mean, don’t get me wrong, I’m sure they are useful. But a certain type of people just LOVE tag clouds. This post isn’t about if they are awesome or if they are lame, but rather about how to make a WordPress tag cloud respect your authority, especially when using query_posts() to trim down the posts on a specific page.
You see, if you are using query_posts() to choose a subset of your posts, you probably don’t want the tags for posts that aren’t on that page to show in that tag cloud. Unfortunately, the default WordPress tag cloud gets the whole frigging taxonomy and that’s no fun for anyone. The approach I took was to make a second loop and snag the tags out of the posts that are in play. This approach might not work for you, but it worked for my needs at the time.
Here’s the code:
function qp_tag_cloud() {
$tags = array();
if ( have_posts() ) : while ( have_posts() ) : the_post();
if (get_the_tags() ) {
foreach(get_the_tags() as $tag) {
$tags[$tag->term_id] = $tag->name;
}
}
endwhile;
endif;
foreach ($tags as $key=>$value) {
echo "<a href="". get_tag_link($key) ."">" . $value . "</a>";
}
}
OK, so this is more of a list of tags than a tag cloud. Anyway. Hope this helps someone, as there was very little helpfulness out in the Google about this issue. Enjoy!