我有这个片段,显示网站中属于特定词汇表的所有分类列表。 而不是打印整个列表,我如何只打印属于我实际加载的节点的术语? 我有一个Drupal 7安装。
这是我打印节点ID的方式:<?php print $node->nid;?>
<?php
$vid = 11; //vocabulary id
$query = "SELECT tid, name, count
FROM (
SELECT td.tid AS tid, name, COUNT(td.tid) AS count
FROM taxonomy_term_data AS td
JOIN taxonomy_index AS tn
ON td.tid = tn.tid
JOIN node AS n
ON n.nid = tn.nid
WHERE td.vid = ". $vid ."
AND n.status = 1
GROUP BY td.tid
ORDER BY count DESC
) AS t
ORDER BY name ASC";
$result = db_query($query);
foreach($result as $term) {
if ($term->count > 0) {
echo l($term->name, "taxonomy/term/$term->tid").' ('.$term->count.')'.'<br/>';
}
}
?>
答案 0 :(得分:2)
我建议不要为此运行额外的查询。 此信息应该在$ node对象中可用。
只需打印它[print_r($ node)],看看分类对象名称究竟是什么($ node-&gt; taxonomy)&amp;分类信息的结构和方式用它来显示节点页面或节点预告片上的类别。
在其他页面上,您可以使用node_load来加载节点,然后执行相同的操作。
答案 1 :(得分:1)
sumoand 的答案更加优化,但是对于一些在这里练习的SQL来说,这是你想象的确切解决方案:
<?php
$vid = 11; //vocabulary id
$query = "SELECT tid, name, count
FROM (
SELECT td.tid AS tid, name, COUNT(td.tid) AS count
FROM taxonomy_term_data AS td
JOIN taxonomy_index AS tn
ON td.tid = tn.tid
JOIN node AS n
ON n.nid = tn.nid
WHERE td.vid = ". $vid ."
AND n.status = 1
AND n.nid = ".$node->id."
GROUP BY td.tid
ORDER BY count DESC
) AS t
ORDER BY name ASC";
$result = db_query($query);
foreach($result as $term) {
if ($term->count > 0) {
echo l($term->name, "taxonomy/term/$term->tid").' ('.$term->count.')'.'<br/>';
}
}
?>