标签云 对我们的文章画龙点睛,如果让我们的标签云随机产生彩色效果,更是增加了不少个性化,我们现在抛弃插件,自己动手用代码来DIY自己的彩色标签云。
这里只介绍最简单也最有效的一款代码,其他的也可以在网上搜索,还有很多的,放到function.php的最后一个?>前面就可以了:
/***圆角背景色标签***/
function colorCloud($text) {
$text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
return $text;
}
function colorCloudCallback($matches) {
$text = $matches[1];
$colors = array('F99','C9C','F96','6CC','6C9','37A7FF','B0D686','E6CC6E');
$color=$colors[dechex(rand(0,7))];
$pattern = '/style=(\'|\")(.*)(\'|\")/i';
$text = preg_replace($pattern, "style=\"display: inline-block; *display: inline; *zoom: 1; color: #fff; padding: 1px 5px; margin: 0 5px 5px 0; background-color: #{$color}; border-radius: 3px; -webkit-transition: background-color .4s linear; -moz-transition: background-color .4s linear; transition: background-color .4s linear;\"", $text);
$pattern = '/style=(\'|\")(.*)(\'|\")/i';
return "<a $text>";
}
add_filter('wp_tag_cloud', 'colorCloud', 1);
代码如上,你可以对style参数进行调整,但是默认的就已经非常漂亮了
感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
暂无评论内容