WordPress无插件实现彩色标签云
WordPress无插件实现彩色标签云。将下面代码复制到主题模板文件function中即可。其中(rand(888,888888))的数字可以修改为你喜欢的颜色区域。
// 实现彩色标签云
function colorCloud($text) {
$text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
return $text;
}
function colorCloudCallback($matches) {
$text = $matches[1];
$color = dechex(rand(888,888888));
$pattern = '/style=(\'|\")(.*)(\'|\")/i';
$text = preg_replace($pattern, "style=\"color:#{$color};$2;\"", $text);
return "<a $text>";
}
add_filter('wp_tag_cloud', 'colorCloud', 1);