实现Wordpress彩色标签云
在相应主题的functions.php加入下面的代码,位置刻意在?>前面:
function colorCloud($text) {
$text = preg_replace_callback('||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 "";
}
add_filter('wp_tag_cloud', 'colorCloud', 1);
修改Wordpress彩色标签字体大小,排序,显示数量等。
文件位置在wp-includes下的category-template.php 文件中,搜索wp_tag_cloud,找到相关参数进行修改:
function wp_tag_cloud( $args = '' ) {
$defaults = array(
'smallest' => 12, 'largest' => 16, 'unit' => 'pt', 'number' => 50,
'format' => 'flat', 'separator' => "\n", 'orderby' => 'count', 'order' => 'DESC',
'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
);
$args = wp_parse_args( $args, $defaults );
$tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) );
注:
smallest表示标签的最小字号
largest表示最大字号
unit=px表示字体使用像素单位
number=0表示显示所有标签,如果为40,表示显示40个
orderby=count表示按照标签所关联的文章数来排列
order=DESC表示降序排序(ASC表示升序排序,DESC表示降序排序)
Wordpress彩色标签云小工具调用。
保存之后回到首页就可以看到彩色标签云的效果了。