Separate 'free tagging' terms from normal taxonomy terms

Normally, Drupal nodes list the taxonomy terms they're associated with as a 'category' or 'topic.' With the introduction of free tagging vocabularies in Drupal 4.7, however, those category lists can be cluttered up by the various keyword tags that are associated with a node.

The following snippet, when pasted into your theme's template.php file, separates Free tagging terms from normal ones for separate theming.

Drupal 5

<?php
function _phptemplate_variables($hook, $vars) {
 
$variables = array();
  if (
$hook == 'node') {
    if (
module_exists('taxonomy')) {
      foreach (
taxonomy_get_vocabularies($vars['node']->type) as $vid=>$vocab) {
        foreach (
taxonomy_node_get_terms_by_vocabulary($vars['node']->nid, $vid) as $term) {
          if (
$vocab->tags) {
           
$tag_links['tag_link-'. $term->tid] = array(
             
'title' => $term->name,
             
'href' => taxonomy_term_path($term),
             
'attributes' => array('rel' => 'tag','title' => strip_tags($term->description)),
            );
          }
          else {
           
$term_links['term_link-'. $term->tid] = array(
             
'title' => $term->name,
             
'href' => taxonomy_term_path($term),
             
'attributes' => array('rel' => 'tag','title' => strip_tags($term->description)),
            );
          }
        }
      }
    }
    else {
     
$term_links = array();
     
$tag_links = array();
    }

   
$variables['terms'] = theme('links', $term_links);
   
$variables['tags']  = theme('links', $tag_links);
  }
  return
$variables;
}
?>

Drupal 4.7

<?php
function _phptemplate_variables($hook, $vars) {
 
$variables = array();
  if (
$hook == 'node') {
    if (
module_exist('taxonomy')) {
      foreach (
taxonomy_get_vocabularies($vars['node']->type) as $vid=>$vocab) {
        foreach (
taxonomy_node_get_terms_by_vocabulary($vars['node']->nid, $vid) as $term) {
          if (
$vocab->tags) {
           
$tag_links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => $term->description));
          }
          else {
           
$term_links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => $term->description));
          }
        }
      }
    }
    else {
     
$term_links = array();
     
$tag_links = array();
    }

   
$variables['terms'] = theme('links', $term_links);
   
$variables['tags']  = theme('links', $tag_links);
  }
  return
$variables;
}
?>

Now, in your node.tpl.php, the $terms variable contains 'normal' taxonomy terms and the $tags variable contains free-tagging terms. Happy theming!

Another approach

<?php
function _mytheme_get_taxonomy($node)
{
   
$base_path = base_path();
   
$vocabularies = array();
   
$output = '';
  
    foreach (
$node->taxonomy as $term)
    {
       
$vocabularies[$term->vid][$term->name] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
    }
  
    foreach (
$vocabularies as $vid => $terms)
    {
       
$vocabulary = taxonomy_get_vocabulary($vid);
      
       
$output .= '<li class="taxonomy vocabulary-'. $vid .'">';
       
$output .= '<img src="'. $base_path .'files/icons/taxonomy-'. $vid .'.png" alt="" />';
       
$output .= ' <span class="description">' . $vocabulary->name . ':</span> ';
       
$output .= theme('links', $terms);
       
$output .= '</li>';
    }
  
    return
$output;
}
?>

Now you just have to insert $vars['myterms'] = _mytheme_get_taxonomy($vars['node']); into _phptemplate_variables() and there you go.

how to used in drupal6?

ctz556 - August 9, 2008 - 07:45

It can not work in drupal6.x.
how to used in drupal6?

I'm interested in this for

Fayna - December 27, 2008 - 02:18

I'm interested in this for D6 as well. Subscribing. :-)

I found a solution for this

Fayna - December 27, 2008 - 03:32

I found a solution for this in D6 in an article by Caroline Schnapp: http://11heavens.com/putting-some-order-in-your-terms. Just remember to clear your cache afterwards. :-)

Right

kardave - May 22, 2009 - 16:06

The right solution, thanks!

 
 

Drupal is a registered trademark of Dries Buytaert.