- "Ask the Expert" or Advice Column
- "Must Have" Modules
- A Challenge
- Books Overview
- Changing Garland - A Practical Example
- Comparison of Links and Web Links modules
- Create Simple Tables
- Creating a "Biographies" page
- Developing a Module on a Windows System
- Generic Table Display
- How to page a custom DB query
- List Users From a Single Role in a Block
- My Modules
- SBS Web Site Notes
- Searching for a New Hosting Company
- Taxonomy Tidbits
- Theming a Specific Content Type
Vocabulary Term Count with Columns
Submitted on Fri, 11/16/2007 - 18:48.
This is a sample of how to create a list of all terms that are being used from a particular vocabulary (category).
I found 16 terms to put into 2 columns for you.
|
|
Here's the code.
<p>This is a sample of how to create a list of all terms that are being used from a particular vocabulary (category).</p>
<?php
$vid = 1; /* <---- put correct vocabulary ID here */
$num_cols = 2; /* <---- put number of columns here */
$show_pic = module_exists('taxonomy_image');
echo '<div class="use-pin">';
$items = array();
$terms = taxonomy_get_tree($vid);
foreach ( $terms as $term ) {
$count = taxonomy_term_count_nodes($term->tid);
if ($show_pic) { $pic = taxonomy_image_display($term->tid /*, 'align="left"'*/); }
else {$pic = NULL; }
$name_and_count = l($pic . $term->name,'taxonomy/term/'.$term->tid, NULL, NULL, NULL, NULL, TRUE)." (".$count.") - ".$term->description;
$items[] = $name_and_count .'<div class="clear-block"></div>';
} /* end foreach */
if (count($items)) {
if ($num_cols == 1) {
echo theme('item_list', $items);
}
else {
$slice = ceil(count($items) / $num_cols);
echo '<table cellpadding="10"><tr>';
for ($i = 0; $i < $num_cols; ++$i) {
$start = $i * $slice;
$class = $i % 2 ? 'even' : 'odd';
echo '<td class="'. $class .'" valign="top">';;
echo theme('item_list', array_slice($items, $start, $slice));
echo '</td>';
}
echo '</tr></table>';
}
}
else { echo 'No terms found'; }
echo '</div>';
?>










This code is nice, but seems to be listing full nodes
How can I list teasers only.
Thank you
Not nodes
This code lists taxonomy terms, not nodes. The stuff you see above is all part of the term descriptions and Taxonomy Image.
Don't show terms with 0 count
Hi Nancy,
Thank you for providing us with these great scripts!
I have tried the script on this page and it works perfectly, only thing is it also displays empty terms -- is there a way to avoid that?
This script of yours does just that: http://drupal.org/node/146043#comment-234365, using count.
I just don't know how to combine it to make it work... do you have a solution?
Thank you,
Nico
Try this
Right after the line that says
$count = taxonomy_term_count_nodes($term->tid);add a lineif (!$count) { continue; }I think that will do it.
It worked perfectly. Again,
It worked perfectly.
Again, thank you so much!
How to display only vocabulary and level 1 terms?
I have put your code and it works perfectly, but how to modify the code, so it will display like this :
I have tree structure category :
AUTOMOTIVE (vocabulary)
** Cars(terms level 1)
***** Mazda (terms level 2)
****** Mitsubishi (terms level 2)
****** Mercedez (terms level 2)
** Motorcycle (terms level 1)
****** Honda (terms level 2)
****** Kawasaki (terms level 2)
****** Yamaha (terms level 2)
and I only need to show like this (vocabulary with level 1 terms):
AUTOMOTIVE (vocabulary)
** Cars (terms level 1)
** Motorcycle (terms level 1)
How to modify the code?
Thank you.
Chris
Hmm...
$max_depth = 1;$terms = taxonomy_get_tree($vid);foreach ( $terms as $term ) {
$count = taxonomy_term_count_nodes($term->tid);
$terms = taxonomy_get_tree($vid);foreach ( $terms as $term ) {
if ($term->depth > $max_depth) { continue; }
$count = taxonomy_term_count_nodes($term->tid);
Only showing terms level 1 with no vocabulary
I have update the code, and now it only show term level 1 , and vocabulary not showing, any suggestion? Thanks
-Chris-
Yes, change the depth
The line at the top that says "$max_depth = 1;" controls how deep into the vocabulary it will go.
I can't get is works even I
I can't get is works even I changed "$max_depth = 1;" or 2, 3..only shows level 1 terms.