四处学习看到的代码,学习收藏了。还是偏爱彩色的标签云
方法如下:
后台编辑主题的 function.php
文件 加入以下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $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);
|
颜色是随机的,可以自行修改此行 $color = dechex(rand(0,16777215));
在侧边栏 sidebar.php 里调用如下代码:
<?php wp_tag_cloud('smallest=8&largest=24&number=50'); ?>
8 是最小的 tag 的字体大小(用的最少的 tag),24 是最大的(用的最多的 tag),50 是 tag 数目,可自行修改。
第二段代码是自己规定颜色的标签云:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| function colorCloudCallback($matches) {
$tag_link=$matches[1];
$colorFull = array(‘
‘
$color=$colorFull[ mt_rand(0, count($colorFull) - 1)];
$pattern = ‘/style=(’|”)(.*)(’|”)/i’;
$tag_link= preg_replace($pattern, “style=”color:{$color};$2;”", $tag_link);
return “<a $tag_link>”;
}
add_filter(‘wp_tag_cloud’, ‘colorCloud’, 1);</blockquote> $colorFull就是自己规定的颜色,可自行添加
|