|
有不少的朋友挺喜欢那种效果的,所以我就把如何实现彩色标签云显示的方法写出来。
其实实现标签云彩色显示的方法很简单,就是通过向 functions.php 文件添加一段代码来实现的。
添加的代码如下:
[pre]
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);
[/pre]
注意:如果你使用的主题没有 functions.php 文件,那么你需要在你使用的主题目录中新建一个 functions.php 文件,不过别忘记在开始处添加 <?php 与在结尾处添加?> 。
|
|