WordPress彩色背景标签云实现方法(无需插件)缩略图标签云

1.在调用的地方加入以下代码:
<aside class=”tags”><?php wp_tag_cloud(‘smallest=12&largest=12&number=45&order=DESC’); ?></aside>
标签不同的话,请设置class为tags
如果你想在边栏中通过文本小工具直接添加的话,需要文本小工具支持PHP代码,实现的方法阅读文章 让你的WordPress文本小工具运行PHP

2.在你的主题style.css添加以下css样式
.tags{padding: 12px 13px 10px 15px;}
.tags a:nth-child(9n){background-color: #4A4A4A;}
.tags a:nth-child(9n+1){background-color: #428BCA;}
.tags a:nth-child(9n+2){background-color: #5CB85C;}
.tags a:nth-child(9n+3){background-color: #D9534F;}
.tags a:nth-child(9n+4){background-color: #567E95;}
.tags a:nth-child(9n+5){background-color: #B433FF;}
.tags a:nth-child(9n+6){background-color: #00ABA9;}
.tags a:nth-child(9n+7){background-color: #B37333;}
.tags a:nth-child(9n+8){background-color: #FF6600;}
.tags a{opacity: 0.80;filter:alpha(opacity=80);color: #fff;background-color: #428BCA;display: inline-block;margin: 0 5px 5px 0;padding: 0 6px;line-height: 21px}
.tags a:hover{opacity: 1;filter:alpha(opacity=100);}

3、wordpress纯代码实现圆角彩色背景标签云
在当前主题目录下面的functions.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);

作者 billxie

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注