2

我想创建一个类别术语列表(包含在一个块中),每个术语后面跟着一个具有该术语的节点数的计数,例如:

Cats (5)
Dogs (4)
Elephants (2)

有许多模块可以动态创建像这样的整个列表,但我发现它们对于我的目的都有缺点。我实际上只需要这样的东西:

<ul>
<li><a href="mylink">Cats</a> (<?php ...some code... ?>)</li>
<li><a href="mylink">Dogs</a> (<?php ...some code... ?>)</li>
<li><a href="mylink">Elephants</a> (<?php ...some code... ?>)</li>
</ul>

即我只需要动态计数,而不是整个列表(这没关系,因为条款本身不会改变)。我听说 Drupal 函数taxonomy_term_count_nodes()可能很有用,但我找不到任何关于其实现的简单信息。

4

6 回答 6

8

您想要关于其实施的哪些信息?文档似乎很清楚......

<?php print taxonomy_term_count_nodes($term_id); ?>
于 2009-10-28T18:46:15.093 回答
3

我已经做过几次了。IMO 你最好使用 Views 创建一个仅列出分类术语的节点视图,然后使用http://drupal.org/project/views_groupby模块将该术语的节点数放在术语旁边。views_groupby 文档中的示例告诉您需要了解的内容。

您的自定义模块显然会更灵活一些,但考虑到上述情况,最终没有必要。

于 2009-10-28T21:15:52.763 回答
2

我也不确定您在文档中缺少哪些信息 - 也许一个示例会有所帮助。下面将创建一个词汇表中所有术语的有序列表,并附上它们的节点数(未经测试,因此可能存在拼写错误)。

// Adjust this to the id of the vocabulary holding your terms
$vid = 1;
// Grab all terms in that vocabulary
$terms = taxonomy_get_tree($vid);
$items = array();
foreach ($terms as $term) {
  // Get the number of (published) nodes with that term
  $count = taxonomy_term_count_nodes($term->tid);
  // Assemble your link text
  $text = $term->name . ' (' . $count . ')';
  // Set this to the path you want to link to (default term views used here)
  $path = 'taxonomy/term/' . $term->tid;
  // Turn the above into a rendered link
  $link = l($text, $path);
  // Add to items
  $items[] = $link;
}
// Render array as an ordered list
$list = theme('item_list', $items, NULL, 'ol');

print $list;
于 2009-10-28T23:14:28.787 回答
1

Taxonomy_term_count_nodes在 Drupal 7 中不存在

Drupal 7 有另一个等效的 api 来获取分类术语的节点。这是 api https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/funct ...

它将返回数组值,我们可以用 php 计算数组值

<?php 
  $data = taxonomy_select_nodes($tid);
  $count = count($data);
于 2015-06-09T05:50:32.207 回答
0

在 Drupal 7 中,您实际上可以使用 Views 来做到这一点。无需编码或附加模块。请参阅如何使用视图聚合器创建分类术语计数块?详情。

于 2013-08-06T14:38:11.367 回答
0

如果您使用 taxonomy_select_nodes 仅获得 10 个结果,那是因为 Pager 在此函数中默认设置为 true,因此您需要使用 'FALSE' 执行此操作:

$data = taxonomy_select_nodes($term_id, FALSE);
$count = count($data);
于 2015-12-24T08:57:08.963 回答