没想到CCK的写了好几篇,怎么写都写不完,还好终于勉强的搞定了.现在让我们接着第5篇继续往下写.我们现在要采取的方式就是使用一段代码,将这些东西输出了.
我们写一个函数,用来读取分类以及它下面的内容,然后直接在页面文章中调用这个函数,这是一个很高效的方法,稍微会点php的都会这样干,我见过不少以前搞php的人,开始使用drupal时,都是直接使用裸php函数,无论是节点中,区块中,还是任何可以使用php code输入格式的地方,他们都会使用php。不过在drupal6.x中,我们这里6.5,php代码这种输入格式被取消了,大量的在内容中使用php代码,在drupal圈内是个被严重鄙视的方式。
Php代码放到文件,放到哪里?最好放到template.php文件中。也就是你的主题的template.php文件中,我们这里是garland。我们先来看看garland的template.php文件,里面有phptemplate_body_class(),phptemplate_breadcrumb(),phptemplate_comment_wrapper(),phptemplate_preprocess_page(),phptemplate_menu_local_tasks(),phptemplate_comment_submitted($comment),phptemplate_node_submitted($node),phptemplate_get_ie_styles()这样的函数,列出来这么多就是建议如果有时间的话,就看看这些函数,它们是干什么的。包括函数的命名方式,用途。有时候你需要修改这些函数。
我们写个函数,phptemplate_show_taxonomy_($tid),根据分类术语输出下面的内容,函数中内容就是读取术语ID $tid,根据ID得到术语的名称,已经使用该术语作为标签的节点列表。下面是我写的函数。
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;
}
//$term_name = db_result(db_query($sql),$tid);
//$output是用来拼html的,把html片段和数据拼起来
//print_r($term_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,2);
//循环,输出节点标题,这里加了链接了,注意l()函数的使用
while($data = db_fetch_object($result)){
$output .="<li>".l($data->title,"node/$data->nid")."</li>";
}
//加上结束标签
$output .= "</ul></div>";
//ok返回
return $output;
}
注意我中间的改动,最初我用的是$term_name = db_result(db_query($sql),$tid),但是发现在这里不行,就改成了:
$result = db_query(db_rewrite_sql($sql),$tid);
while($data = db_fetch_object($result)){
$term_name = $data->name;
}
现在在page-front.tpl.php中插入3个语句:
<div class="clear-block">
<?php print phptemplate_show_taxonomy_(1) ?>
<?php print phptemplate_show_taxonomy_(2) ?>
<?php print phptemplate_show_taxonomy_(3) ?>
<?php //print $content ?>
</div>
同时注掉$content的输出,就可以了。效果如下: