What i am trying to do is echo the last hierarchial child term for the taxonomy. My taxonomy is propertytype
.
My hierarchial structure is:
- Real Estate (Parent)
- Commercial (Child)
- Offices (Child of Commercial)
- Basement (Child of Commercial)
So in the above case i would only like to echo Offices
, or which ever the last hierarchial child term is.
I thought my code below would do this, but it actually echoes Commercial
because Commercial
is alphabetically before Offices. Now Basement
will be ok because it is alphabetically before Commercial
.
How do I always echo the last hierarchial child term only?
My code is:
$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->slug;
}
}
}
echo $props[0];