Attempting to follow Lynn's method for displaying taxonomy terms broken out by vocabulary, the following API function call seems to return an empty array. I must be missing something. I'm attempting to get a list of terms from vocabulary with vid=4.

$terms = taxonomy_node_get_terms_by_vocabulary($nid, 4);

Below please find the complete function where this shows up in the template.php file. Examining print_r( $terms ) reveals that the array is empty, and thus the if-statement is never entered. (Running Drupal 6.x, PHP 5.1.4.) Thank you for any suggestions.

function phptemplate_print_terms($nid) {
  $items = array();
  $terms = taxonomy_node_get_terms_by_vocabulary($nid, 4);
  if ($terms) {
      $links = array();
      foreach ($terms as $term) {
          $links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
      }
      $output = implode(', ', $links);
      $items[] = $output;
  }
  return theme('item_list', $items);
} 

Lynn's method for displaying taxonomy terms broken out by vocabulary:
http://drupal.org/node/133223#comment-774663

Comments

Chill35’s picture

Try this tutorial: http://11heavens.com/putting-some-order-in-your-terms

The method I show there is quite different, and works really well.

Caroline
11 heavens.com

lrickard’s picture

Thanks, Chill35. It's a great tutorial, and it allowed me to successfully theme the terms I needed. Thanks for posting such helpful information.

That said, I'd love to know why the taxonomy_node_get_terms_by_vocabulary() function was returning an empty array. If anyone has thoughts on the code above, I'll keep my eye on this thread.

Chill35’s picture

The first parameter must be a node object, not a node id.

Call the function this way:

$node = node_load($nid);
$terms = taxonomy_node_get_terms_by_vocabulary($node, 4);
lrickard’s picture

You're a rockstar! Thank you for the help. Passing the node object absolutely worked, but I think I'm going to stick with your method for now. I've commented the PHP Snippet thread to reflect this conversation. I guess the api function switched its parameters between Drupal 5 and Drupal 6.
http://drupal.org/node/133223

scottrigby’s picture

Hi Caroline,

This is a fantastic turorial!

I just have one issue: Even after breaking out vocabularies, the the order of the vocabs themselves are still in alphabetically sorted by the terms within them (so if Vocab 1 has term 'Saturn' and Vocab 2 has term 'Apple', then group 2 will display first.

This is a problem for me for two reasons:
* I want the vocabularies to show in the same order for visual consistency
* Also because I'm using pathauto to generate the node's path according to a token for the top-level term. Ideally there could be some way to be sure that the first term within the Vocab sorted at the top of the taxonomy sort order (in admin/content/taxonomy) will always be the first term.

Do you have any advice for modifying the code to account for this?

Here's the code I'm using from your tutorial BTW (slightly adapted to Zen theme's _preprocess_node function):

/**
 * Override or insert variables into the node templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("node" in this case.)
 */
function dlc1_preprocess_node(&$vars, $hook) {
  //$vars['sample_variable'] = t('Lorem ipsum.'); KEEP COMMENTED OUT FOR NOW
  // BELOW IS FROM 11heavens.com/putting-some-order-in-your-terms FOR BREAKING OUT TERMS BY VOCAB
  // If we have any terms...
  if ($vars['node']->taxonomy) {
    // Let's iterate through each term
    foreach ($vars['node']->taxonomy as $term) {
      // We will build a new array where there will be as many
      // nested arrays as there are vocabularies
      // The key for each nested array is the vocabulary ID     
      $vocabulary[$term->vid]['taxonomy_term_'. $term->tid]  = array(
        'title' => $term->name,
        'href' => taxonomy_term_path($term),
        'attributes' => array(
          'rel' => 'tag', 
          'title' => strip_tags($term->description)),
        );       
    }
    // We will get rid of the old $terms variable
    unset($vars['terms']);
    // And build a new $terms
    foreach ($vocabulary as $vid => $terms) {
      // Getting the name of the vocabulary
      $name = taxonomy_vocabulary_load($vid)->name;
      // Using the theme('links', ...) function to theme terms list
      $terms = theme('links', $terms, array('class' => 'links inline'));
      // Wrapping the terms list
      $vars['terms'] .= '<div class="vocabulary taxonomy_vid_';
      $vars['terms'] .= $vid;
      $vars['terms'] .= '">';
      $vars['terms'] .= $name;
      $vars['terms'] .= ':&nbsp;';
      $vars['terms'] .= $terms;
      $vars['terms'] .= '</div>';
    }
  }   
}

Thanks in advance for any thoughts on how to deal with this :)

Scott

Chill35’s picture

I would use CSS or jQuery. I provide a class name that is different for each vocabulary, like so:

taxonomy_vid_ID1

taxonomy_vid_ID2

taxonomy_vid_ID3

Where IDn is the vocabulary id.

You can float one to the left and the other to the right. You can position theme absolutely, as well.

You can also use one line of jQuery (presentational jQuery if you will) to reorder them.

Caroline
11 heavens.com

Chill35’s picture

You may also want to try adding this line... not tested.

    // We will get rid of the old $terms variable
    unset($vars['terms']);
    // Sorting the $vocabulary array
    ksort($vocabulary, SORT_NUMERIC);
    // And build a new $terms
    foreach ($vocabulary as $vid => $terms) {

Caroline
11 heavens.com

Jeff Burnz’s picture

Chill35’s picture

Great! :-)

Caroline
11 heavens.com

scottrigby’s picture

Hi Caroline,
this is great - at least to know how to sort by ID.

But what if we want to use the Taxonomy Vocabulary sort order (ajax draggable sort order at /admin/content/taxonomy/list)?

Do you know what we'd use in place of SORT_NUMERIC?
can we use D6 Api theme_taxonomy_overview_vocabularies
http://api.drupal.org/api/function/theme_taxonomy_overview_vocabularies/6
If so, do you have any suggestions about could these integrate into your function?

Thanks :)
Scott

scottrigby’s picture

After looking at the API, I tried this - and it seems to work so far:

    if (isset($vocabulary['weight'])) {
      ksort(($vocabulary['weight']), SORT_NUMERIC);
    }

Caroline, do you see any immediate problems with going this route?

Scott Rigby
http://basekamp.com
http://PlausibleArtworlds.org

Jeff Burnz’s picture

Hi heather, do you have a moment?

I used your preprocess method (which is great by the way), on my localhost it works fine, but on my live testing server I get the error unexpected T_OBJECT_OPERATOR in line XXX

Which is...

  $name = taxonomy_vocabulary_load($vid)->name;

Main difference between the two servers is that my localhost runs PHP5 (where it works) and the testing server is PHP4 (no worky).

Any advice?

francort’s picture

have you tried to explode it into two lines?

$my_vocabulary = taxonomy_vocabulary_load($vid);
$name = $my_vocabulary->name;
Jeff Burnz’s picture

summit’s picture

Hi,
Subscribing.
Is it also possible to get terms from different vocabularies also in the url, something like pathauto but then generated from different vocabularies?
Greetings, Martijn