Taxonomy Term Count

This is a sample of how to create a list of all terms that are being used from a particular vocabulary (category).

  • Attendance Matrix (1) - This provides a table view of who attended which meetings.
    Project page | Documentation page | Change log
  • FAQ_Ask (14) - This module is an add-on to the FAQ module that allows users with the 'ask question' permission to create a question which will be queued for an 'expert' to answer.
    Project page | Documentation page | Change log
  • Get_Content_Type (1) - The get_content_type module fills an oversight by the D5 developers. When they moved the part of CCK (sometimes called CCK-Lite) into core for creating new content types, they forgot the analog to taxonomy/term/xxx, that is node/type/xxx. This simple module provides that function.
    This module has not been contributed due to disparaging comments from more advanced Drupallers.
    Project page & Documentation
  • Glossary (26) - The glossary module scans content for glossary terms (including synonyms). The glossary indicator is inserted after every found term, or the term itself is turned into an indicator, depending on the site settings. By hovering over the indicator, users will see the definition of that term displayed as a "tool tip." Clicking the indicator leads the user to that term presented within the whole glossary or directly to the detailed description of the term, if available.
    Project page | Documentation | Change log
  • Gotcha (8) - Gotcha intercepts the Contact form submission and checks an inserted hidden field to catch spambots. If something is there, Gotcha simply returns to the front page and ignores the message. The attempt is logged and saved in the database. If the field is empty, then the message is scanned by the Spam module, which has very good filters for catching spam. If everything looks okay, the message is passed on through to the Contact module for normal processing.
    Project page | Documentation | Change log
  • Helpers (8) - Helpers is a collection of functions to help developers work smarter and faster.
    Project page | Documentation page | Change log
  • Longer Titles (2) - Drupal 6 changed the length of the title field for nodes to 255. This module brings that change to version 5.x.
    Project page | Documentation | Change log
  • Node_Type_Filter (4) - This is a simple module that allows for various lists, such as "taxonomy/term," to be filtered by content type.
    Project page | Documentation page | Change log
  • Register_Country (1) - Are you creating a country portal or a site dedicated to a specific country?
    The Register Country module is designed to intercept new registrations and check if the IP address being used is registered to a country that the site administrator has chosen. In this way, you may limit sign ups to your site to specific countries.
    Project page | Documentation page | Change log
  • Site Documentation (39) - To simplify it a bit, the Site Documentation module picks up information from various places within the Drupal environment. Some of the information comes from internal arrays, some is derived from system calls, and some comes directly from the database tables. This information is pulled into a report that can be used to document the site. In addition, it will detect some problems that may exist in your installation, and optionally correct them.
    Project page | Documentation page | Change log
  • Site Notes (14) - SiteNotes introduces a new content type, called, coincidentally, "sitenotes." It also creates a menu item in the Admin » Site building menu, where it's available only to privileged users; it's even protected by Access Control. So now all those little Post-Its™ and other scraps of paper can go right into your database where you can find them again. Create "How To" notes for your users, theme changes, CSS modifications, special code notes, and keep them safe AND available.
    Project page | Documentation | Change log
  • Spam Tokens (2) - This add-on module adds another tab to the Spam administration page that allows Spam module administrators to examine and modify the tokens used by the Bayesian filters to determine the probability of content being spam.
    Project page | Documentation | Change log
  • Spam_Tune (1) - The Spam_Tune module allows Spam module administrators to examine and modify system variables that are normally just defaulted in the Spam module. These variables are added to the 'Advanced' tab in the Spam settings. This module will probably never be contributed as it can be dangerous to alter the parameters exposed here.
    Project page & Documentation
  • Taxonomy Browser (8) - Think of this as a 'build your own category view' page. A single page with each term organized nicely by vocabulary. The user selects the terms which she or he wants to see, and then this module constructs the right URL (e.g. taxonomy/view/and/3,4,5) and then displays matching nodes to the user.
    Project page | Documentation page | Change log
  • Taxonomy Delegate (1) - This module allows an administrator with "administer taxonomy" permission to delegate the administration of a vocabulary to a non-admin role.
    This module has not yet been contributed. We are looking for beta-testers.
    Project page & Documentation
  • Taxonomy Image (5) - Allow an administrator to associate image with taxonomy terms or vocabularies for display with the terms.
    Project page | Documentation page | Change log
<?php
  $vid = 2; /* <---- put correct vocabulary ID here */
  $items = array();
  $terms = taxonomy_get_tree($vid);
  foreach ( $terms as $term ) {
    $count = taxonomy_term_count_nodes($term->tid);
    if ($count) { /* don't show terms with 0 count */
      $items[] = str_repeat('-', $term->depth) . l($term->name,'taxonomy/term/'.$term->tid)." (".$count.") - ".$term->description;
    }
  } /* end foreach */
  print theme('item_list', $items);
?>

Comments

View the hierarchy

Hi Nancy,

Thanks! This is a great pice of code that I really like! However, I'm trying to modify it so that the list displayed by the code also shows the hierarchy of the terms. I.e. if the Category has the following terms and children terms:

Partent 1
-- Child 1
-- Child 2
Parent 2
Parent 3
-- Child 1

Then I would like the List to also reflect this relationship, by e.g. having the children slightly indent. Now the output list just displays:

- Partent 1
- Child 1
- Child 2
- Parent 2
- Parent 3
- Child 1

Unfortunately I'm just a Drupal newbie and after having spent hours trying to modify your code to do the thing I just mentioned I still haven't succeeded. Do you have any suggestions on how such a modification could be made?

Thanks!

/Henrik

You got it

I modified the sample code to do what you wanted. I used only one dash per level, but you can change the "str_repeat" to make it more, or change the character to be used.

Thanks!

That's really great! I had no idea it was so simple. Thanks a lot!

Tweak for missing descriptions

I use free tagging and don't often (read: hardly ever) go back to add descriptions to terms. I ended up with a lot of lines like:

foo (1) -
bar (1) - some description
baz (3) -

I changed the line generating code to:

     if ($count) { /* don't show terms with 0 count */
       $line = l($term->name,'taxonomy/term/'.$term->tid)." (".$count.")";
       if ($term->description) {
         $line = $line.": ".$term->description;
       }
       $items[] = $line;
     }<

so that the final output looks like:

foo (1)
bar (1): some description
baz (3)

Thanks for the snippet! That was simpler than I had feared.

Thank you very much for the great share...

Hey Nancy,
Thank you very much for the great share...
A question on this [I’m not a programmer or coder :)]
With this code my current output is as follow:
< a > Term1 < / a> (5)
< a > Term2 < / a> (8)
< a > Term3 < / a> (2)
Etc…

My Requirement is
< a > Term1 (5) < / a>
< a > Term2 (8) < / a>
< a > Term3 (2) < / a>

Any idea?
thanks again!!

Sure

Change:
$items[] = l($term->name,'taxonomy/term/'.$term->tid)." (".$count.") - ".$term->description;<

To:
$items[] = l($term->name,'taxonomy/term/'.$term->tid ." (".$count.")") ." - ".$term->description;<

It is untested, but I think it's right.

How to add a pager

Hi Nancy
is possible to add a pager?

Yes

thank you so much, this

thank you so much, this saved me hours of frustration!

Block for specific node

Thanks Nancy for this. A great help so far. But then this provides a block ACROSS all no types. I want to create a block which will only be for "story" nodes. Is there any way to do that?

Thanks.

Well, actually

Actually, this only counts the nodes for which the vocabulary is applicable. If the vocabulary is defined only for "story" type, then only "story" would be counted. However, if it's defined for "story" and "page" then it will count both.

I do have another method that separates the count by type, but I forgot to put it up on this site. I will do that in a moment. It will be in the "Drupal Collection" and titled "Content count by Taxonomy Term."

language i18n

Hello,

Can I lookup the translated term, because with this code, only the term_data are shown and if you are using the i18n module, the translated term is in localizertranslation. How can it be done?

Hope someone knows a solution.

TIA,
Fossie

I wish I knew

Unfortunately I speak only English so I don't know anything about i18n support. However, the taxonomy module code that gets invoked looks like it might translate. Try it. If not, I suggest posting on Drupal.org.

I've also posted it on

I've also posted it on Drupal.org, but because I use your fantastic code to display the taxonomy terms within a certain vocabulary, I've also posted it on your site.

To explain in short:
I use standard taxonomy support
1 vocabulary
Lookup terms (at least 1 element - your code)
Display taxonomy_image and term->name

I have i18n enabled and translated terms (for french)

If I'm visiting the page (Egnlish or French) in only shows the English term, data that's looked up in term_data (db table).

I wait until I recieve some comments on Drupal.org.

Thx anyway,
Fossie