Warning message

The service having id "digg_smart_button" is missing, reactivate its module or save again the list of services.

Print taxonomy terms without links in Drupal 7

In Drupal taxonomy terms are displayed by default as links to the teaser views. Sometimes, the links themselves are not relevant within the context of the content and you need to display the taxonomy terms as pure text.

Say in your node--doctors.tpl.php you need to display all the doctor specialties. The doctor specialties machine name in the doctors content type is field_doctor_specialities and it is a term reference for the taxonomy vocabulary “doctor specialties”.

I have written a generic function to display the field taxonomy terms without the links and you can simply call it in your node--contenttype.tpl.php file, while passing the taxonomy term field as the first parameter and the separator between the taxonomy terms as the second optional parameter.

Here is the function:

function YOURTHEME_print_terms($variables, $sep=" | ") {
                //define main variables and reset them
                $i=1;
                $output="";  //the final output variable
                $class="";  // the class of spans
                $array_size= count($variables['#items']);
                //loop through the taxonomy terms
                foreach ($variables['#items'] AS $term){ //
                                if ($i==1){$class .=" first";}
                                if ($i%2==0){$class .=" even";}else {$class .=" odd";}
                                if ($i==$array_size){$class .=" last";}
                               
                                $output .= ' '.$term['taxonomy_term']->name.'';
                                //put seperator for all but not last item
                                if ($i<>$array_size){
                                $output .=' '.$sep.'';
                                }
                                $i++;
                                $class="";//reset class for spans
                }
                return $output;
}

 

Now, in our node—doctors.tpl.php we can use the function by printing the following code:

print YOURTHEME_print_terms($content['field_doctor_specialities']);