Is there an easier way to print taxonomies in the tpl? I get this working code, but it should be a "shorter" way! After a painful day and a half I get this code, that prints out in my node.tpl.php the taxonomy terms associated to that particular node that the user is viewing.

It works just fine. As it is quite long I´ll just put the whole thing inside a module and into a variable, to just print that.

The thing is that it should get an easier, better way to do this!
Any ideas?

&lt;?php 
$vid = 11; //vocabulary id
$nid = $node-&gt;nid; //it looks for the current loaded node
$query = "SELECT tid, name
FROM (
SELECT td.tid AS tid, name
FROM taxonomy_term_data AS td
JOIN taxonomy_index AS tn
  ON td.tid = tn.tid
JOIN node AS n
  ON n.nid = tn.nid
WHERE td.vid = ". $vid ."
  AND n.status = 1
  AND n.nid = ".$nid."
GROUP BY td.tid
) AS t
ORDER BY name ASC";
$result = db_query($query);
foreach($result as $term) {
      echo l($term-&gt;name, "taxonomy/term/$term-&gt;tid") . ', ';
}
 ?&gt;


UPDATE:

This is devel output:
(I´ve used (dpm($node);) because it let me print the result here without losing the indentation)

(object) array(
  'vid' =&gt; '5178',
  'uid' =&gt; '1',
  'title' =&gt; 'PROYECTO',
  'log' =&gt; '',
  'status' =&gt; '1',
  'comment' =&gt; '2',
  'promote' =&gt; '0',
  'sticky' =&gt; '0',
  'nid' =&gt; '155',
  'type' =&gt; 'jornadas',
  'language' =&gt; 'und',
  'created' =&gt; '1095048000',
  'changed' =&gt; '1360589684',
  'tnid' =&gt; '0',
  'translate' =&gt; '0',
  'revision_timestamp' =&gt; '1360589684',
  'revision_uid' =&gt; '1',
  'taxonomy_vocabulary_4' =&gt; array(),
  'taxonomy_vocabulary_6' =&gt; array(
    'und' =&gt; array(
      array(
        'tid' =&gt; '33',
        'taxonomy_term' =&gt; (object) array(
            'tid' =&gt; '33',
            'vid' =&gt; '6',
            'name' =&gt; 'Jornadas Gratuitas',
            'description' =&gt; '',
            'format' =&gt; NULL,
            'weight' =&gt; '0',
            'vocabulary_machine_name' =&gt; 'vocabulary_6',
          ),
      ),
    ),
  ),
  'taxonomy_vocabulary_7' =&gt; array(
    'und' =&gt; array(
      array(
        'tid' =&gt; '40',
        'taxonomy_term' =&gt; (object) array(
            'tid' =&gt; '40',
            'vid' =&gt; '7',
            'name' =&gt; 'Para PH',
            'description' =&gt; '',
            'format' =&gt; NULL,
            'weight' =&gt; '-10',
            'vocabulary_machine_name' =&gt; 'vocabulary_7',
          ),
      ),
    ),
  ),
  'taxonomy_vocabulary_11' =&gt; array(
    'und' =&gt; array(
      array(
        'tid' =&gt; '262',
        'taxonomy_term' =&gt; (object) array(
            'tid' =&gt; '262',
            'vid' =&gt; '11',
            'name' =&gt; 'colegios',
            'description' =&gt; '',
            'format' =&gt; NULL,
            'weight' =&gt; '0',
            'vocabulary_machine_name' =&gt; 'vocabulary_11',
          ),
      ),
      array(
        'tid' =&gt; '543',
        'taxonomy_term' =&gt; (object) array(
            'tid' =&gt; '543',
            'vid' =&gt; '11',
            'name' =&gt; 'derecho',
            'description' =&gt; '',
            'format' =&gt; NULL,
            'weight' =&gt; '0',
            'vocabulary_machine_name' =&gt; 'vocabulary_11',
          ),
      ),
    ),
  ),
  'body' =&gt; array(
    'und' =&gt; array(
      array(
        'value' =&gt; "
Few things here:


Your SQL is open to injection
You're running a query that has already been run, and for which data has already been returned by the core field system. Drupal already runs way too many queries in a page load, adding another unnecessary one should be avoided wherever possible.
Your code should be in a template preprocess function (or module file as you've mentioned). But that's not because of the length of the code. It's because you should maintain a good separation of logic and display.
Drupal has already prepared the display of the field for you, so it would be preferable to use that instead of building up from scratch again.


With those in mind, try simply adding:

&lt;?php echo render($content['field_name_of_term_ref_field']); ?&gt;


in place of your code.

To control how those field items come out (e.g. as links), navigate to admin/structure/types/manage/TYPE/display, and choose appropriate settings for the display widget.