自定义函数显示 Drupal 分类列表
MiMi Posted on 2009.12.09 20:06 // 4 Comments
Drupal中的CCK和View模块功能强大,几乎可以用鼠标点出你想要的任何内容。
但耗费的性能和效率问题也与它的功能一样强大。
其实CCK和View能实现的我们手动一样可以实现,而且异常简单。
以国内网站最常见的显示某一分类最新文章列表为例:
在模板文件 template.php 中增加一个自定义函数如下:
function phptemplate_show_taxonomy($tid){
//根据tid读取术语名称
$sql ="SELECT name FROM {term_data} WHERE tid = %d";
$result = db_query(db_rewrite_sql($sql),$tid);
while($data = db_fetch_object($result)){
$term_name = $data->name;
}
$output = "<div>";
$output .= "<h2>".$term_name."</h2><ul>";
$sql = "SELECT n.nid, n.title, n.created
FROM {term_node} tn
LEFT JOIN {node} n on n.nid = tn.nid
WHERE tn.tid = %d
ORDER BY n.created DESC";
$result = db_query_range(db_rewrite_sql($sql),$tid,0,8);
while($data = db_fetch_object($result)){
$output .="<li>".l($data->title,"node/$data->nid")."</li>";
}
$output .= "</ul></div>";
return $output;
}
在模板适当位置写入:
<?php print phptemplate_show_taxonomy(12) ?>
function phptemplate_show_taxonomy($tid){
//根据tid读取术语名称
$sql ="SELECT name FROM {term_data} WHERE tid = %d";
$result = db_query(db_rewrite_sql($sql),$tid);
while($data = db_fetch_object($result)){
$term_name = $data->name;
}
$output = "<div>";
$output .= "<h2>".$term_name."</h2><ul>";
$sql = "SELECT n.nid, n.title, n.created
FROM {term_node} tn
LEFT JOIN {node} n on n.nid = tn.nid
WHERE tn.tid = %d
ORDER BY n.created DESC";
$result = db_query_range(db_rewrite_sql($sql),$tid,0,8);
while($data = db_fetch_object($result)){
$output .="<li>".l($data->title,"node/$data->nid")."</li>";
}
$output .= "</ul></div>";
return $output;
}
在模板适当位置写入:
<?php print phptemplate_show_taxonomy(12) ?>
PS:1年前做Drupal项目时写得备忘,今天被不小心翻出来了…
WordPress贴代码真麻烦,不过懒得修改了,过几天把自己的Blog软件完善下发布出来…
如无特别说明 本站所有作品皆为觅觅原创 转载请注明出处 觅觅守候 MiMiz.cn
TrackBack: http://mimiz.cn/index.php/drupal/drupal-category-list/trackback/