What i am trying to do is echo the last hierarchial child term for the taxonomy. My taxonomy is 'propertytype'propertytype
.
My hierarchial structure is:
Real Estate (Parent)
Real Estate (Parent)Commercial (Child)
Commercial (Child)Offices (Child of Commercial)
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.
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 CommercialCommercial
because CommercialCommercial
is alphabetically before Offices. Now BasementBasement
will be ok because it is alphabetically before commercialCommercial
.
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];
Thanks
$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];