Skip to main content
deleted 55 characters in body
Source Link
mor7ifer
  • 8.6k
  • 2
  • 19
  • 32

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];

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];

Thanks

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];
Source Link
PaulB
  • 43
  • 1
  • 5

Why does my taxonomy code display the first alphabetical term?

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];

Thanks