wordpress用简易代码实现彩色标签云效果,代码不是很多,可以实现这个功能,不想加代码,可以用wordpress插件Colorful Tag Cloud实现,一起学习wordpress代码知识。
第一步、在function.php中添加以下代码,调用标签云是就会显示彩色标签了
1 2 3 4 5 6 7 8 9 |
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); //$text = preg_replace($pattern, "", $text); return "<a $text>";} add_filter('wp_tag_cloud', 'colorCloud', 1); |
填入相应的位置。
第二步,在需要标签云的地方用代码调用
1 |
<?php wp_tag_cloud('smallest=15&largest=40&number=50&orderby=count'); ?> |
可以放入文章页面,如果用小工具需要装Enhanced Text Widget这款wordpress插件识别php代码。