Community Documentation

Display parent taxonomy term

Last updated April 13, 2012. Created by drm on April 13, 2012.
Log in to edit this page.

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 now 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.

<?php
$p
= taxonomy_get_parents($entity->field_myterm['und'][0]['tid']);
foreach(
$p AS $parent) {
 
$entity_field[0]['value'] = $parent->name;
}
?>

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Audience
Programmers
Level
Intermediate
Keywords
taxonomy term

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.