4

Is it possible to create a view to only see the top level terms in a vocabulary? I can't seem to get it to stop listing all terms, using a vocabulary ID argument. I just want to see the top level parents.

Laxman13
  • 5,226
  • 3
  • 23
  • 27
Kevin
  • 13,153
  • 11
  • 60
  • 87

3 Answers3

6

This blog post outlines how to do it: http://www.raisedeyebrow.com/2011/01/show-only-top-level-terms-in-a-term-type-drupal-view/

Essentially you need to add a relationship of term parent. Then add a filter for term name and select is empty (null) using the parent relationship. Effectively only showing terms that have no parent.

Mark Reilly
  • 353
  • 2
  • 5
Reece Marsland
  • 256
  • 1
  • 5
1

You can add a filter of Taxonomy: Term ID and manually choose which terms to show (may be tedious if you have a large vocabulary).

OR

You could add a template file for a field in your view to decide what terms to show. For example, in your view, you could simply add a field of Taxonomy: Term ID. Copy views-view-field.tpl.php to your theme folder from the views module directory (under theme). Go to "Theme information" under "Basic settings" and find a suitable name for the template and create a new file using that name. For example, mine was views-view-field--tax--tid.tpl.php.

To only show term names of the terms that are the top level of a vocabulary, use the following (or similar) in your new template file:

<?php 
  if (count(taxonomy_get_parents($output, $key = 'tid')) == 0) {
    $term = taxonomy_get_term($output, $reset = FALSE);
    print $term->name;
  }
?>
Laxman13
  • 5,226
  • 3
  • 23
  • 27
0

Yes its possible but not sure with views.Below is one way to get the top level terms in vocabulary.

$tree = taxonomy_get_tree($vocabulary_id, 0, -1, 1);

taxonomy_get_tree returns a flat array of terms so you can use that while printing.

Regards, Chintan.

Chintan
  • 136
  • 5