用代碼讓wordpress標籤雲,變成漂亮的標籤雲
點擊數: 2013
文章目錄
我這邊提供二種樣式給看家使用,
第一種樣式
不用外掛,用代碼實現漂亮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);
第二種樣式
實現帥氣wordpress的標籤雲做法
用代碼實現帥氣wordpress的標籤雲,開啟主題的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);
註:若想在任意地方加入標籤雲,可以使用標籤雲的代碼
如下
- <?php wp_tag_cloud(); ?>
- <?php wp_tag_cloud(‘smallest=8&largest=22′); ?>