彩色標簽云我們在很多的網站都可以看到此類效果了,下面我來給現在使用最多的個人博客系統wordpress來介紹如何實現彩色標簽云.
這種彩色標簽云效果可以通過修改Simple Tags來顯示。
1.在simple-tags.client.php中先找到如下代碼:
function getColorByScale($scale_color, $min_color, $max_color)
2.注釋掉(或者刪除)getColorByScale這個function中的以下語句:
- $scale_color = $scale_color / 100;
- $minr = hexdec(substr($min_color, 1, 2));
- $ming = hexdec(substr($min_color, 3, 2));
- $minb = hexdec(substr($min_color, 5, 2));
- $maxr = hexdec(substr($max_color, 1, 2));
- $maxg = hexdec(substr($max_color, 3, 2));
- $maxb = hexdec(substr($max_color, 5, 2));
- $r = dechex(intval((($maxr - $minr) * $scale_color) + $minr));
- $g = dechex(intval((($maxg - $ming) * $scale_color) + $ming));
- $b = dechex(intval((($maxb - $minb) * $scale_color) + $minb));
3.加上以下代碼:
- //Colorful Tag Cloud start
- $r = dechex(rand(0,255));
- $g = dechex(rand(0,196));
- $b = dechex(rand(0,255));
- //Colorful Tag Cloud end至于要顯示多少個標簽,怎么排列,熱門標簽和普通標簽分別為多少大小的字體,可以在后臺的Simple Tags的選項中設置.
方法二,如果上面辦法不可以我們可參考如下辦法。
后臺編輯 主題 的 functions.php,輸入以下代碼:
- 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);
可以看到,顏色是隨機的,可以自行修改 $color = dechex(rand(0,16777215)); 這行來修改范圍,這樣就很炫耀哈哈,然后在側邊欄 sidebar.php 里調用如下代碼:
<?php wp_tag_cloud('smallest=8&largest=24&number=50'); ?>
即可,8 是最小的 tag 的字體大小(用的最少的 tag),24 是最大的(用的最多的 tag),50 是 tag 的數目,可以自行修改.
新聞熱點
疑難解答
圖片精選