dashan 提交于
Drupal中的CCK和View模块功能强大,几乎可以用鼠标点出你想要的任何内容。
但耗费的性能和效率问题也与它的功能一样强大。 :mrgreen:
其实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) ?>
- 登录以发表评论
Drupal中判断当前页面为分类页的方法
在Drupal中,分类系统路径都是类似这种格式:
taxonomy/term/10
taxonomy/term/17
那么,根据Drupal API,判断当前页面为分类页面,可以这样写:
if ( arg(0) == ‘taxonomy’ && arg(1) == ‘term’ ) {
echo ‘这是分类';
} else {
echo ‘这不是分类';
}
- 登录以发表评论
dashan 答复于 永久连接