The following code will display the child taxonomy term of the parent only.
$terms = get_the_terms($wp_query->post->ID, 'propertytype');
$props = array();
foreach ($terms as $term) {
if ($term->parent)
$props[] = $term->name;
}
echo implode(', ', $props);
For example: London (Parent), West London (Child) - the above will only echo West London (Child).
However, if there are multiple child levels e.g London (Parent), West London (Child), Chelsea (Grand Child)
This does not work! I only want to display the last term.
How would i modify the above to only display the last term even if there is more than one sub level?
Thanks
Paul
FINAL CODE USED
$terms = get_the_terms($wp_query->post->ID, 'propertytype');
//die(print_r($terms));
$props = array();
foreach ($terms as $term) {
$hasChildrenTest = get_term_children($term->ID, 'propertytype');
if ($term->parent) {
if (empty($hasChildrenTest) && !is_wp_error($hasChildrenTest)) {
$props[] = $term->name;
}
}
}
echo $props[0];