0

I have registered one custom taxonomy. I am using this code to show this taxonomy on single.php.

But it shows parent and child terms as well.

function showorganisation()
{
    $terms = get_the_terms($post->ID, 'organization');
    foreach ($terms as $term) {
        $term_link = get_term_link($term, 'organization');
        if (is_wp_error($term_link))
            continue;
        echo '<a href="' . $term_link . '">' . $term->name . '</a>';
    }
}

How can I make it show parent terms only?

1 Answer 1

0

get_the_terms() gives you an array of WP_Term objects. The object has parent (int) property, which tells you, if the term is a child and has a parent term or not.

This means that in your loop you can skip child terms with a simple if statement.

foreach ($terms as $term) {

  // Is it a child term?
  if ( $term->parent ) {
    continue;
  }

  // code...

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.