WordPress添加彩色标签云(Tag Cloud)添加方法,打开当前主题的functions.php文件,加入以下代码:
function colorCloud($text) { $text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text); return $text; } function colorCloudCallback($matches) { $text = $matches[1]; $color = dechex(rand(0,16777215)); $pattern = '/style=('|")(.*)('|")/i'; $text = preg_replace($pattern, "style="color:#{$color};$2;"", $text); return "<a $text>"; } add_filter('wp_tag_cloud', 'colorCloud', 1);
添加完成之后,即可在需要显示彩色标签云的地方加入以下调用代码:
<?php wp_tag_cloud('smallest=8&largest=24&number=50'); ?>
代码参数说明:smallest=8&largest=24&number=50
8表示字号、字体大小,24表示调用最热门的24个标签,50表示一共输出50个标签。