WordPress标签云彩色背景代码

在functions.php中添加如下代码即可:

/* 彩色静态标签云 Color Tag Cloud
/* -------------------------------- */
function colorCloud($text) {
$text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
$text=preg_replace('/<a /','<a ',$text);
return $text;
}
function colorCloudCallback($matches) {
$text = $matches[1];
//这里定义我们背景色的范围,如果不想设置背景色的输出范围我们可以使用
//$color = dechex(rand(0,16777215));从所有颜色中随机出一个
$a = array('8D7EEA','F99FB2','AEE05B','E8D368','F75D78','55DCAB','F75DB1','ABABAF','7EA8EA');
$co = array_rand($a,2);
$color = $a[$co[0]];
//随机颜色代码结束,下面开始把颜色赋值给每个标签生成背景色
$pattern = '/style=(\'|\")(.*)(\'|\")/i';
$text = preg_replace($pattern, "style=\"background:#{$color};\"", $text);
return "<a $text>";
}
//把php代码挂载到wp_tag_cloud钩子上
add_filter('wp_tag_cloud', 'colorCloud', 1);

发表评论