Is it possible to list taxonomy terms using views or anything similar?

Example: I have a gallery as the vocab name, which has "landscape, seascape, portrait" etc as the terms.

would like to list these names with the first node image as a teaser.

Comments

nancydru’s picture

No need for Views. Just create a php page:

<?php
  $vid = 1;         /* <---- put correct vocabulary ID here */
  $terms = taxonomy_get_tree($vid);      /* need code from below to handle nesting */
  print "<ul>";
  foreach ( $terms as $term ) { 
      $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d ", $term->tid));
      if ($count) {   /* don't show terms with 0 count */
         print "<li>".$term->name." (".$count.")</li>";
       }
   } /* end foreach */
  print "</ul>";
?>

And it's clear you didn't search the forums very hard before asking this. I've posted this at least 3 times in the last two weeks.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in Your Database

iancawthorne’s picture

Thanks, I did have a very similar php code, but was hoping it might be able to be configured with views rather than creating a php page.

This code lists the terms, which is close, but I'd like to pull the teaser of the first node in that term (an image thumbnail in this case)

An example would be, I have a term type of 'landscapes' in my vocab type 'gallery'.

I have 4 nodes attatched to this term type, the first being 'Scottish Mountain Range' with a thumbnail.

I'd like to pull through that thumbnail to go next to the name 'Landscapes' in that list to make it more visual.

Hydrant Ltd
www.hydrant.co.uk

nancydru’s picture

This is supposed to be do-able with Views, I think. I prefer this method because it's faster, but many people prefer to rely on Merlin's code.

Yours is an interesting concept, however. It might give the "icon-ized" menu look that a lot of people have asked for. This is something that I might consider for a module I'm working on.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in Your Database

fossie’s picture

Hello,

This code works fine for the term name in the term_data table, but can it also read the translated terms?

Hope someone knows a solution.

TIA,
Fossie

fossie’s picture

nobody has a clue?

Fossie

fossie’s picture

nobody has a clue?

Fossie

hc11’s picture

fossie - are you trying to do something similar to what I have on the right side of my page:

http://the.singularitynetwork.org

?

Antimattar’s picture

how did you do it?

also can it limit the number of terms listed?

nancydru’s picture

The code I posted above can just as easily be put into a block, which is what it looks like he did.

I suppose you could limit the number of terms, but it is in alphabetical order, so you won't see the Z entries.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

neelanjana das’s picture

In the admin panel i can only see the machine name of the vocabulary. where can I find the vid?

nancydru’s picture

You can go into the database and find it. If you need it in code, you can use <a href="https://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.module/f..."> taxonomy_vocabulary_machine_name_load($name)</a>.

neelanjana das’s picture

I have found a solution through code which I have posted below for others to get a help from. Thank you for your reply :) . 

mrgoltra’s picture

subscribing

uuuuut’s picture

can this code be modified so it will recognize parent and child categories?

sunnygambino’s picture

It will be show links to terms of taxonomy:

for ($i=1;$i<=200;$i++) {  
$vid = $i;         /* <---- all (now max. 200) vocabulary ID here */
  $terms = taxonomy_get_tree($vid);      /* need code from below to handle nesting */
  print "<ul>";
  foreach ( $terms as $term ) {
      $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d ", $term->tid));
      if ($count) {   /* don't show terms with 0 count */
         print '<li><a href="?q=taxonomy/term/'.$i.'">'.$term->name.' ('.$count.')</a></li>';
       }
   } /* end foreach */
  print "</ul>";
}

I hope it will be usefull. :)

staryi’s picture

Greetings all.
Now use this code in the block,

<? php 
$ vid = 1; / * <---- put correct vocabulary ID here * / 
$ terms = taxonomy_get_tree ($ vid); 
foreach ($ terms as $ term) ( 
$ tcount = taxonomy_term_count_nodes ($ term-> tid); 
$ children_terms = taxonomy_get_children ($ term-> tid); 
if ($ term-> depth == 0) ( 
print l ("-".$ term-> name, 'taxonomy / term /' 
. $ term-> tid, array ( 'title' => $ tcount. "posts in" 
. $ term-> name)); 
print ( "<br>"); 
) 
) 
?> 

But I do not know how to do filtering by language, to render all the terms, although they mutually serve each other translations
-Africa
-Asia
-Australia and Oceania
-Europe
-North America
-South America
-Австралия и Океания
-Азия
-Африка
-Европа
-Северная Америка
-Южная Америка

How display terms only the language of the desired vocabulary taxonomy? (Using - drupal 6*, i18n )

vin247’s picture

Almost what I was looking for, this code displays the the term id incorrectly as it's set to display the vid so the terms don't link correctly, for anyone having the same problem I used following:

print '

  • tid.'">'.$term->name.' ('.$count.')
  • ';

    in place of

    print '

  • '.$term->name.' ('.$count.')
  • ';

    picxelplay’s picture

    I suggest using this tutorial instead:
    http://drupal.org/node/770782

    ----
    Sudo Kill Cylons

    sheba’s picture

    Thanks for posting this great code.
    This is code for Drupal 7 that worked fo me.

      $vid = 2;         /* <---- put correct vocabulary ID here */
      $terms = taxonomy_get_tree($vid);      /* need code from below to handle nesting */
    dsm($terms);
      echo "<ul>";
    foreach ( $terms as $term ) {
        $count = db_query("SELECT COUNT(nid) FROM {taxonomy_index} WHERE tid = :tid ", array(':tid' => $term->tid))->fetchField(); 
    
          if ($count) {   /* don't show terms with 0 count */
             print "<li>".$term->name." (".$count.")</li>";
           }
    }
      echo "</ul>";
    
    Jasonrj’s picture

    I tried using this code, but what is the dsm() function? I was getting errors until I took that line out, then it worked fine.

    alexkb’s picture

    It's a debugging function provided by the contributed module devel.

    In terms of producing this listing in views, check out this thread: http://drupal.org/node/476116

    neelanjana das’s picture

    put this code in the php file you want to print the taxonomy term list.

    <?php 
     $name = "gallery";      /* machine name of your vocabulary */

    /*load vocabulary by name. you can find the find the machine name in  the /admin/structure/taxonomy/. click on edit. you will find the machine name in that page.   */
     

    $voclist = taxonomy_vocabulary_machine_name_load($name); 
        $vid = $voclist->vid;
        $terms = taxonomy_get_tree($vid);

    foreach($terms as $t){
        echo $t->name . "<br>";    /*prints the raw data(terms) from your vocabulary */
    }
     ?>