拜访了不少WordPress博客,发现一部分博客的标签云是彩色的,配合那些比较鲜艳多彩的主题看起来挺好看,之前博客吧在《WordPress 博客flash标签云插件WP-Cumulus》中介绍了WordPress博客flash标签云插件,本篇博客吧介绍的是代码实现WordPress博客彩色标签云的方法。
彩色标签云实现方法步骤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //Start of colorful tag cloud 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); return "<a $text>"; } add_filter('wp_tag_cloud', 'colorCloud', 1); //End of colorful tag cloud |
1 | <?php wp_tag_cloud( 'smallest=8&largest=24&number=50' ); ?> |
代码参数提示:
1 | $color = dechex(rand(0,16777215)); |
提醒:可以不使用上面的标签云调用代码,直接使用原主题的也可以。