0

I am trying to echo the custom post's child "section", if one exists. I only want to show the second level, or child, sections. This is working, however, it displays the 1st level section if no second level exists.

$terms = get_the_terms($post->ID, 'section' );
if ($terms && ! is_wp_error($terms)) :
    $term_slugs_arr = array(
    );
    foreach ($terms as $term) {
        $term_slugs_arr[0] = $term->name;
    }
    $terms_slug_str = join( " ", $term_slugs_arr);

endif;

echo '<p class="sub-section">'.$terms_slug_str.'</p>'; 

1 Answer 1

1

Try this may be help you.

$terms = get_the_terms($post->ID, 'section' );

echo '<ul>'; 
      foreach ($terms as $term) {
           $term_slugs_arr[0] = $term->name;
           $termchildren = get_term_children( $term->term_id, 'section' );
           foreach ( $termchildren as $child ) {
                  $term = get_term_by( 'id', $child, $taxonomy_name );
                  echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
    }
}
echo '</ul>';

OR

Check this Link

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.