Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Whether to assign only the lowest level taxonomy term to a page, or also assign the parent terms so that they display, is a frequent issue. This snippet allows you to display a parent term of a term already assigned to your entity. Note that this is not actually a term reference, so doing this will not allow you to list this page using the taxonomy system.
But if your term reference field is called myterm (field_myterm), you can use this to display a parent term. This assumes that there is just one parent term.
$p = taxonomy_get_parents($entity->field_myterm['und'][0]['tid']);
foreach($p AS $parent) {
$entity_field[0]['value'] = $parent->name;
}
Comments
how to get parent terms of
how to get parent terms of selected terms in a multi value field group by root?