The bellow comes from thread http://drupal.org/node/133223
split out taxonomy terms by vocabulary.
It works But it only shows the bottom level of taxonomy terms.

The Question I have is how do I use the second code combined with this code?
Because the second snippet spits out the terms correctly just in one long vertical list with no vocabularies.

function my_theme_print_terms($nid) {
     $vocabularies = taxonomy_get_vocabularies();
     $output = '<ul>';
     foreach($vocabularies as $vocabulary) {
       if ($vocabularies) {
         $terms = taxonomy_node_get_terms_by_vocabulary($nid, $vocabulary->vid);
         if ($terms) {
           $links = array();
           $output .= '<li>' . $vocabulary->name . ': ';
           foreach ($terms as $term) {
             $links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
           }
           $output .= implode(', ', $links);
           $output .= '</li>';
         }
       }
     }
     $output .= '</ul>';
     return $output;
}

This is the address of my site with the above code. http://bowwowimport.com/products/boge-pro-gas-front-strut-insert.

what I want to use as part of this code but I need to have the $vocabularies = taxonomy_get_vocabularies();
and show all the parents under Car Manufacturer formatted like the above code does but with all the parents of the years .
and this is how the above code outputs the taxonomy.

my taxonomy is like this:
Car Manufacturer is the vocabulary
-volkswagen
--Jetta
---MKIII
----1999
----1998
----1997
----ETC
---MKIV
----2003
----2002
----ETC

The nodes fit multiple years of different models but not always the same years.

function my_theme_print_terms($nid) {
  $terms = taxonomy_node_get_terms($nid);
  $links = array();
  $processed = array();
  if($terms){
    foreach($terms as $term){
      $parents = taxonomy_get_parents_all($term->tid);
      foreach($parents as $parent){
        if(!in_array($parent->tid, $processed)){
          $links[] = l($parent->name, taxonomy_term_path($parent), array('rel' => 'tag', 'title' => strip_tags($parent->description)));
          $processed[] = $parent->tid;
        }
      }
    }
    $output = theme('item_list', $links);
    return $output;
  }
}

I am getting close to getting this.

Thanks Eric (bowwowadmin)

Remember always keep the sun on your back and Drupal in your face :)

Comments

lonehorseend’s picture

I've only documented the stuff I added. I did this directly in a node.tpl file replacing the <span class="taxonomy"><?php print $terms?></span> line instead of going through the template.php.
I took part of my code from here: http://drupal.org/node/53089. Also, it works only if there is one vocabulary involved. If there are two or more, it creates a "See also:" line for every vocabulary. I'm working on a version to correct this problem.

<?php
if ($page == 0) {
$termtoexclude=arg(2); //figure out what term page you are looking at
$countofterms = 0; //this is to allow the skipping of terms
$vocabs = array();
foreach( (array)$node->taxonomy as $term ) {
  $vocab = taxonomy_get_vocabulary($term->vid);
  $parents = taxonomy_get_parents_all($term->tid); //get the parents of the current term in the list of terms
  if ( !isset($vocabs[$vocab->name]) ) {
  $vocabs[$vocab->name] = array();
  }
  if ($term->tid != $termtoexclude) //make sure the current page term isn't in the list of terms
  {
  if (count($parents) > 0) //make sure there is at least one level of parents.  If not just use the current term
  {
  $vocabstemp = array(); //need this because taxonomy_get_parents_all starts with the current term and works backwards, so you have to reverse the order.
  $vocabsfinal='';
  //build your temporary list of parents
  foreach ($parents as $parent) {
  $vocabstemp[] .= l($parent->name, "taxonomy/term/$parent->tid");
  }
  $vocabstemp = array_reverse($vocabstemp); //reverse the list of parents so that it makes sense to the human eye.
  //build your final list of parents making sure to take out the seperator on the last term
  foreach ($vocabstemp as $vocabstemp2key => $vocabstemp2) {

  if ($vocabstemp2key < count($vocabstemp)-1)
  {
  $vocabsfinal .= $vocabstemp2.">";
  }
  else 
  {
   $vocabsfinal .= $vocabstemp2;
  }
  }
  $vocabs[$vocab->name][] = $vocabsfinal;
  }
  else
  {
    $vocabs[$vocab->name][] = l($term->name, "taxonomy/term/$term->tid");
  }
  ++$countofterms;
  }
}
//print the term list only if it isn't the current page term.
if ($countofterms > 1) {
foreach ( $vocabs as $name => $termlinks ) {
  print '<div class="taxonomy"> See Also: ';
  print implode(', ', $termlinks);
  print '</div>';
  }
}
}?>
lonehorseend’s picture

This is for those cases where a node has a list of related terms with spanning over 2 or more categories. I've only documented the changes from above.

<?php
$termtoexclude=arg(2);
//We don't want to print anything if all it is repeating either the current term or vocabulary. The next 3 lines are the setup for this.
$printvocab=FALSE; 
$vocabtermtoskip = taxonomy_get_term($termtoexclude);
$vocabtoskip = taxonomy_get_vocabulary($vocabtermtoskip->vid);
$vocabs = array();
foreach($node->taxonomy as $term ) 
	{
	$vocab = taxonomy_get_vocabulary($term->vid);
	$parents = taxonomy_get_parents_all($term->tid); 
	if ($vocab->name != $vocabtoskip->name)//If we aren't skipping the current page's vocabulary, we are putting the vocabulary in front of the term path.
		{
		$vocabs[$vocab->name]['vocab']=l($vocab->name, "taxonomy/vocabulary/$term->vid").">";
		}
	else
		{
		$vocabs[$vocab->name]['vocab']="";
		}
	if ($term->tid != $termtoexclude)
		{
		$printvocab=TRUE;
		if (count($parents) > 0)
			{
			$vocabstemp = array();
			$vocabsfinal='';
			foreach ($parents as $parent) 
				{
				$vocabstemp[] .= l($parent->name, "taxonomy/term/$parent->tid");
				}
			$vocabstemp = array_reverse($vocabstemp);
			foreach ($vocabstemp as $vocabstemp2key => $vocabstemp2) 
				{
				if ($vocabstemp2key < count($vocabstemp)-1)
					{
					$vocabsfinal .= $vocabstemp2.">";
					}
				else 
					{
					$vocabsfinal .= $vocabstemp2;
					}
				}
			 $vocabs[$vocab->name]['links'][] = $vocabsfinal;
			}		
		else
			{
			$vocabs[$vocab->name]['links'][] = l($term->name, "taxonomy/term/$term->tid");
			}
		}
	 }
if ($printvocab) //We are using a string called $termcontent because it was the easiest way to make sure that the commas all fell in the right place.
	{
	$termcontent = '<div class="taxonomy"> See Also: ';	
	foreach ($vocabs as $vocabcatkey => $vocabitems)
		{
		if (count($vocabitems['links'])>0)
			{
			foreach ($vocabitems['links'] as $vocabkey => $vocablinks)
				{
				$termcontent .= $vocabitems['vocab'];
				$termcontent .= $vocablinks;
				$termcontent .= ", ";
				}
			}
		}
	$trimmed = rtrim($termcontent,", ");
	$trimmed .= '</div>';
	print $trimmed;
	}?>
summit’s picture

Subscribing, greetings, Martijn

lonehorseend’s picture

After I posted the code here, I ended up writing a snippet page at http://drupal.org/node/265502 that dealt with not only the single and multiple vocabularies, but multiple hierarchies as well.