看到很多博客都有標籤聚合的功能,於是花了點兒時間找了找,在倡萌博客找到了這個方法。
下面和大家共享下:
首先複製一份你博客主題的 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 頁面就可以了。