1

我有这个片段,它显示了站点中属于特定词汇表的所有分类列表。我如何打印属于我实际加载的节点的术语,而不是打印整个列表?我有一个 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/>';
    }
}
 ?>
4

2 回答 2

2

我建议不要为此运行额外的查询。此信息应该在 $node 对象中可用。

只需打印它 [print_r($node) ] 并查看分类对象名称 ($node->taxonomy) 的确切含义以及分类信息的结构以及使用它在节点页面或节点预告片上显示类别。

在其他页面上,您可以使用 node_load 第一次加载节点,然后执行相同的操作。

于 2013-02-12T18:02:27.897 回答
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/>';
    }
}
?>
于 2013-02-13T16:52:19.720 回答