网摘,记录修改,便于以后重装。
仍functions.php
//圆角背景色标签
function colorCloud($text) {
$text = preg_replace_callback('||i', 'colorCloudCallback', $text);
return $text;
}
function colorCloudCallback($matches) {
$text = $matches[1];
$color = dechex(rand(0,16777215));
$pattern = '/style=(\'|\")(.*)(\'|\")/i';
$text = preg_replace($pattern, "style=\"display: inline-block; *display: inline; *zoom: 1; color: #fff; padding: 1px 5px; margin: 0 5px 5px 0; background-color: #{$color}; border-radius: 3px; -webkit-transition: background-color .4s linear; -moz-transition: background-color .4s linear; transition: background-color .4s linear;\"", $text);
$pattern = '/style=(\'|\")(.*)(\'|\")/i';
return "";
}
add_filter('wp_tag_cloud', 'colorCloud', 1);
上述方法是最普遍的,不过发现有一个弊端,网页一般是白色背景,如果随机颜色是白色的,那么标签看上去就是一个空白,我们可以通过定义颜色范围来决定标签颜色。
//将第一段中的这段代码删除
$color = dechex(rand(0,16777215));
//替换成一下代码,颜色范围可自定义
$colors = array('F99','C9C','F96','6CC','6C9','37A7FF','B0D686','E6CC6E');
$color=$colors[dechex(rand(0,7))];
调用标签云,smallest表示最小字体,largest表示最大字体,unit表示字体单位pt/px,number表示调取标签的数量
很不错的技术文,留言支持下博主