Support

Account

Home Forums Front-end Issues Can't display a Custom field in parent level taxonomy Reply To: Can't display a Custom field in parent level taxonomy

  • 
    // current term
    $current_term = get_queried_object();
    
    // child terms
    // this returns an array of terms
    $args = array(
      'taxonomy' => 'YOUR TAXONOMY HERE',
      'parent' => $current_term->term_id,
      // you may need other arguments depending on your needs
    );
    $child_terms = get_terms($args);
    
    // you need to maybe loop through the child terms gotte
    // to pick which one you want to use
    // I'm assuming that you only want to use the first one
    
    $child_term = false; // set it to false to begin with
                         // we'll use this later
    if ($child_terms) {
      $child_term = $child_terms[0];
    }
    
    // make a decision
    if ($child_term) {
      // get field value(s) from child term
      $color = get_field('color', $child_term);
    } else {
      // get field value(s) from current term
      $color = get_field('color', $current_term);
    }
    
    // do something with the values
    echo $color;