开发者

How do I create a node count for each category term in Drupal?

I want to create a list of category terms (contained within a block), with each term to be followed by a count of the number of nodes with that term, e.g.:

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

There are a number of modules that create whole lists like this dynamically, but I've found they all have drawbacks for my purposes. I literally just need something like this:

<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>

I.e. I only need the count to be dynamic, not the whole list (this is OK because the terms themselves will not change). I've heard that the Drupal function taxonomy_term_count_nodes() may be useful, but I can't find any开发者_运维技巧 simple information regarding its implementation.


What information do you want regarding its implementation? The documentation seems pretty clear...

<?php print taxonomy_term_count_nodes($term_id); ?>


I've done that a few times. IMO you're better off using Views to create a Node view that just lists Taxonomy terms, and then use the http://drupal.org/project/views_groupby module to put the number of nodes with that term beside the term. The example at the views_groupby documentation tells you what you need to know.

Your custom module will obviously be a bit more flexible, but ultimately unnecessary given the above.


I'm also not sure what information you are missing in the documentation - maybe an example helps. The following will create an ordered list of all terms within a vocabulary, with their node counts attached (untested, so there may be typos).

// 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;


Taxonomy_term_count_nodes not exits in Drupal 7

Drupal 7 has another equivalent api to get taxonomy term's nodes. here is the api https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/funct...

It will return array value, we could count the array values with php

<?php 
  $data = taxonomy_select_nodes($tid);
  $count = count($data);


In Drupal 7, you can actually do this with Views. No coding or additional modules are required. See How To Use Views Aggregator to Create Taxonomy Term Count Block? for details.


If you get only 10 results with taxonomy_select_nodes that is because Pager is set to true by default in this function so you will need to do ti like this with 'FALSE':

$data = taxonomy_select_nodes($term_id, FALSE);
$count = count($data);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜