關(guān)于wordpress自帶的標(biāo)簽云小工具,可以讓我們?cè)谶m當(dāng)?shù)捻?yè)面位置展示我們添加的標(biāo)簽,但默認(rèn)的標(biāo)簽樣式有點(diǎn)難看,我們就在這次的wordpress美化教程中來(lái)美化以下吧。
- 此次美化教程由紙工廠分享,在此感謝紙工廠。
- 以下代碼均添加于主題根目錄下的
functions.php
文件<?php
底部。
注意
本頁(yè)提供的代碼僅供參考,若需部署,還請(qǐng)下載文本文檔中的代碼來(lái)使用。
首先,您可以根據(jù)下面的代碼注釋來(lái)自定義您的標(biāo)簽云顯示內(nèi)容:
//修改WordPress自帶標(biāo)簽云小工具的顯示參數(shù)
add_filter( 'widget_tag_cloud_args', 'theme_tag_cloud_args' );
function theme_tag_cloud_args( $args ){
$newargs = array(
'smallest' => 14, //最小字號(hào)
'largest' => 20, //最大字號(hào)
'unit' => 'px', //字號(hào)單位,可以是pt、px、em或%
'number' => 80, //顯示個(gè)數(shù)
'format' => 'array',//列表格式,可以是flat、list或array
'separator' => "n", //分隔每一項(xiàng)的分隔符
'orderby' => 'name', //排序字段,可以是name或count
'order' => 'RAND', //升序ASC或降序DESC,RAND隨機(jī)
'exclude' => null, //結(jié)果中排除某些標(biāo)簽
'include' => null, //結(jié)果中只包含這些標(biāo)簽
'link' => 'view' //taxonomy鏈接,view或edit
'taxonomy' => 'post_tag', //調(diào)用哪些分類(lèi)法作為標(biāo)簽云
);
$return = array_merge( $args, $newargs);
return $return;
}

方法一
好,如果你想要自己的標(biāo)簽云變的好看的話,您可以參考下面的代碼:
// 實(shí)現(xiàn)彩色標(biāo)簽云
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);

效果如下:

方法二
感覺(jué)還是不夠特色?來(lái)看看這個(gè):
//WordPress圓角彩色背景標(biāo)簽云
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);

效果如下:
