看到很多部落格都有標籤聚合的功能,於是花了點兒時間找了找,在倡萌部落格找到了這個方法。
下面和大家共享下:
首先複製一份你部落格主題的 page.php 模板
然後在其頂部加入
<?php
/*
Template Name: Tags
*/
?>
然後將這種顯示正文內容的程式碼換成呼叫 tags 的程式碼
<?php the_content(); ?>
換成
<?php wp_tag_cloud(『smallest=12&largest=18&unit=px&number=0&orderby=count&order=DESC』);?>
在 style.css 裡面增加如下程式碼可以使得間距更好看:
#page-cnt.tags,
#page-cnt.friends { height: 576px; padding: 6px 0 0; overflow: hidden; line-height: 30px; }
#page-cnt.tags,
#page-cnt.friends { height: auto; padding-top: 5px; overflow: visible; }
.tags a { display: inline-block; margin: 0 4px; white-space: nowrap; }
開啟主題中的 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);
大功告成,在後臺新建一個 tags 頁面就可以了。