增加分类统计功能

2007年04月10日 12:59

昨天刚刚搞定分类,TR就留言说应该要有一个分类统计的功能。想想也是,每个分类后面加上blog的数目的确清楚得多。
Pivot 模板标记文档查了一圈,没有发现能够实现这个功能的,只找到一个 [[livearchive_list]] 标签可以列出所有的分类。
看来只能自己写了。去论坛看了一圈,发现Pivot支持一种叫做snippet的东西,可以定制所需的标签。大概的看了一下,规范并不十分复杂。我所理解的规范如下:

  1. 文件放在extension/snippets目录下

  2. 文件以snippet_开头,后面加上你希望的标签名

  3. 文件里需要公开的函数也以snippet_开头,后面加上标签名,然后可以设定参数,参数在模板中以类似[[标签名:参数:参数]]这样的格式传递


看了一些实例后,我决定自己写一个 livearchive_list 的增强版,就是后面跟着blog数的版本。
最简单当然就是从livearchive_list的代码改了。grep了一下,找到了livearchive_list的代码,在modules/module_snippets.php里。
于是把那段代码复制出来,新建一个自己的snippet,就叫livearchive_list_ex吧。
这是代码,蓝色部分就是我增加的。

<?
//----------------------------------------------------------------------
//FILENAME: snippet_livearchive_live_ex.php
//
// Just like [[livearchive_live]], just add the count of entries of each category
// Usage: [[livearchive_live_ex]]
// Author: ch.linghu
//----------------------------------------------------------------------
function snippet_livearchive_list_ex ($filename="", $format="", $weblog="", $template="") {
global $Weblogs, $Current_weblog, $Cfg, $Paths, $allcats;

if ($filename == "") { $filename = $Paths['pivot_url']."archive.php"; }

if ($format == "") {
$format = "<a href='%file%?c=%catid%&w=%weblogid%&t=%template%'>%cat% (%entriescount%)</a><br />";
}

$allcats = cfg_cats();

if ($weblog == "current") {
$cats = find_cats_in_weblog($Current_weblog);
$weblog = $Current_weblog;
} else if ( ($weblog != "") && (isset($Weblogs[$weblog])) ) {
$cats = find_cats_in_weblog($weblog);
} else {
// use all cats..
$cats = cfg_getarray('cats');
}

#---------------by ch.linghu: Count the entries in category
$myDb = new db();
$entries_array = $myDb->getlist_range("0000-00-00-00-00", "2099-01-01-00-00","","", FALSE);
#---------------


usort($cats, "category_simplesort");

foreach ($cats as $cat) {

// skip cat if it's 'not-public'.
if ($allcats[$cat]['hidden'] == 1) { continue; }

// skip if name is empty
if ($cat == "") { continue; }

// skip cat if it doesn't exist anymore
if (!isset($allcats[$cat])) { continue; }

#----------------by ch.linghu: Count the entries in category
$entries_count=0;
$temp="";
foreach($entries_array as $entry)
{
// Only list published entries, not timed posts or held entreis
if (strcmp($entry['status'], 'publish') != 0)
continue; // skip to next entry

// Only consider entries posted to the requested category
if (in_array($cat, $entry['category']))
$entries_count++;
}
#------------------


$my_output = $format;
$my_output = str_replace('%file%', $filename, $my_output);
$my_output = str_replace('%catid%', para_category($cat), $my_output);
$my_output = str_replace('%cat%', encode_text($cat), $my_output);
$my_output = str_replace('%weblogid%', para_weblog($weblog), $my_output);
$my_output = str_replace('%weblog%', $Weblogs[$weblog]['name'], $my_output);
$my_output = str_replace('%template%', $template, $my_output);
$my_output = str_replace('%entriescount%', $entries_count, $my_output);
$output .= $my_output;
}

return $output;

}

?>


源代码在此下载,右键另存为,将后缀的txt去掉即可。

2条评论

gravatar for 猛禽
赞,这么快就搞定啦,收了。 猛禽 (URL) - 2007年04月10日 13:03

gravatar for tr
好,顶一个。 tr - 2007年04月10日 17:44




Remember personal info?
Notify
Hide email
Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Design downloaded from free website templates.