好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

WordPress实现彩色标签云方法介绍 - WordPress

WordPress实现彩色标签云方法介绍

彩色标签云我们在很多的网站都可以看到此类效果了,下面我来给现在使用最多的个人博客系统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 的数目,可以自行修改.

查看更多关于WordPress实现彩色标签云方法介绍 - WordPress的详细内容...

  阅读:55次