Drupal "Print Taxonomy Terms

I am using node -MyContentType.tpl and I need the ability to "print" only a specific dictionary.

In other words, I would like something like this:

<Title>
<Vocabulary [1]>
<Body> 
<Vocabulary [2]>

      

I think this should be pretty easy, but I am struggling ... Ideally I would like to pass arguments:

  • print the "label" of the dictionary (I'm currently doing this with a module)
  • print this term as link
  • print a description of this term

Hope someone can help me, thanks.

Update:

here is what I have so far:

<?php
foreach ( (array)$node->taxonomy as $term ) {
  // $term->name has the taxonomy term name
  // $term->tid has the taxonomy term id

$TermTitle = $term->name;
$TermDescription = $term->description;

print ($TermTitle);
print $TermDescription;
}
?>

      

Now, can anyone help me to get this to only print the specific vocabulary, thanks.

+2


source to share


4 answers


The Taxonomy Hide module gives you some fancy configurations to hide dictionaries and terms from the display. Unless you have a complex use case that can change over time, you might need to hard-code it.

Take a look at taxonomy_hide_nodeapi()

to see how you can customize it to suit your needs. Interesting snippet:



foreach ($node->taxonomy as $key => $value) {


  if (array_key_exists($value->vid, $hidden)) {


  unset($node->taxonomy[$key]);

+1


source


Another simple solution - you can use the treemenu taxonomy module download here , then it will read the QUICKSTART file from there to learn how to create the minimum number of trees, go to the block and you will find the block for that menu, display it anywhere. (rename block in correct name if there is an error message)



0


source


I would use Views for this.

Import this view to get started

$view = new view;
$view->name = 'taxonomy_terms2';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'term_data';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
  'name' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_taxonomy' => 0,
    'exclude' => 1,
    'id' => 'name',
    'table' => 'term_data',
    'field' => 'name',
    'relationship' => 'none',
  ),
  'phpcode' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'value' => '<?php
$term_link = \'taxonomy/term/\'.$data->tid;
if (arg(0)==\'node\') { // If we\'re on a node, get the node\ term and compare it to this term
  $node = node_load(arg(1));
  foreach ($node->taxonomy as $term) {
    if ($data->tid == $term->tid) {
            $extra_class = "active-term";
    }
  }  
} else if (arg(0)==\'topic\') { // If we\'re on a view, compare the first two arguments of the view\ alias to the term\' alias
  if(drupal_lookup_path(\'alias\', $term_link) == arg(0) . \'/\' . arg(1)) {
    $extra_class = "active-term";
  }
}
print l($data->term_data_name, $term_link, array(\'attributes\' => array(\'class\' => $extra_class)));
?>',
    'exclude' => 0,
    'id' => 'phpcode',
    'table' => 'customfield',
    'field' => 'phpcode',
    'relationship' => 'none',
  ),
));
$handler->override_option('sorts', array(
  'weight' => array(
    'order' => 'ASC',
    'id' => 'weight',
    'table' => 'term_data',
    'field' => 'weight',
    'relationship' => 'none',
  ),
));
$handler->override_option('filters', array(
  'vid' => array(
    'operator' => 'in',
    'value' => array(),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'vid',
    'table' => 'term_data',
    'field' => 'vid',
    'relationship' => 'none',
    'override' => array(
      'button' => 'Override',
    ),
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('footer_format', '5');
$handler->override_option('footer_empty', 0);
$handler->override_option('style_plugin', 'list');
$handler->override_option('style_options', array(
  'grouping' => '',
  'type' => 'ul',
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('filters', array(
  'vid' => array(
    'operator' => 'in',
    'value' => array(
      '49' => '49',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'vid',
    'table' => 'term_data',
    'field' => 'vid',
    'relationship' => 'none',
    'override' => array(
      'button' => 'Use default',
    ),
  ),
));
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);

      

Then just choose your vocabulary

0


source


I used this to print the name of the taxonomy where the dictionary code match is 1. It is hardcoded, but it was good enough for my needs.

// Print the Product Type
// Print Taxonomy terms where vid == 1
foreach ($node->taxonomy as $key => $value) {
  if ( $node->taxonomy[$key] -> vid == 1) {
    print ( $node->taxonomy[$key] -> name );
  }
}

      

0


source







All Articles