Hi, I'm using Drupal 6 with the Nitobe theme (6.x-4.1) and a CCK custom content type. I want to display Taxonomy terms when viewing a node as if they are fields in the node (each field on a separate line, preceded by "Term name:".

I found a function in another post that should do this:

<?php
/**
* Return a list of taxonomy terms with each vocab one its own line.
*/
function node_terms_list($node) {

    $vocabularies = taxonomy_get_vocabularies();
    foreach($vocabularies as $vocabulary) {
        if ($vocabularies) {
            $terms = taxonomy_node_get_terms_by_vocabulary($node->nid, $vocabulary->vid);
            if ($terms) {
                $output .= '<p class="terms"><strong>' . $vocabulary->name . ':</strong> ';
                foreach ($terms as $term) {
                    $output .= l($term->name, 'taxonomy/term/'.$term->tid) . ', ';
                }
                $output = trim ($output,", ").'</p>';
            }
        }
    }

    return $output;
}
?>

but it doesn't seem to work when I call it in my custom node template.

I've tried calling the following function from the Nitobe template.php file:

<?php
function nitobe_separate_terms($terms, $prefix = NULL, $separator = NULL) {
  $prefix    = ($prefix == NULL) ? t('Tags: ') : $prefix;
  $separator = ($separator == NULL) ? t(', ') : $separator;
  $output    = $prefix . preg_replace('!a></li>\s<li!',
                                      "a>{$separator}</li>\n<li", $terms);
  return $output;
}
?>

It of course returns the terms on a single line, separated by commas, not on separate lines. I've tried copying the nitobe_separate_terms function and modifying it. However, my PHP is not very strong, so I break the code and the page does not display.

Does anyone know why the first function would not work? Can anyone take some time to help me out here?

Thank you!

Comments

Anonymous’s picture

Status: Active » Postponed (maintainer needs more info)

I'm not sure where the CCK field comes into play here. Are you using a CCK field to store taxonomy terms? Or are you using setting terms on the node using the normal taxonomy controls on the node edit form?

The value in $terms that is being passed into nitobe_separate_terms is already a string containing HTML. The HTML is an unordered list with each term as a list element. The theme's CSS causes these to be rendered inline so that they all appear on the same line.

You will need to write a function that replaces occurrences of <li> in $terms with <li>Term name:

biglazy’s picture

The function: taxonomy_node_get_terms_by_vocabulary() has changed in drupal 6.x, you used it in the way for drupal 5 or lower, click here see the api's change : http://api.drupal.org/api/drupal/modules--taxonomy--taxonomy.module/func... . Notice the first parameter.

You should change this line :

$terms = taxonomy_node_get_terms_by_vocabulary($node->nid, $vocabulary->vid);

into:

$terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid);