Each term object has a parent
attribute which is set to 0 in case it’s a parent root term or the ID of the parent in case it is a child term. So, if you have only one level of child terms then you can check for that attribute to not be equal to 0 in which case the term has a parent.
$object_terms = wp_get_object_terms( $post->ID, 'types', array( 'fields' => 'all' ) );
if ( $object_terms ) {
echo '' . '' . '' ; /* This line is completely useless. */
$res="";
foreach ( $object_terms as $term ) {
/* If parent would be 0 then this 'if' would evaluate to false */
if ( $term->parent ) {
$res .= $term->name . ','; /* You probably wanted ', ' here. */
}
}
/* This is great. In this form the 'rtrim' is useless.
The two "concatenations" are null and completely useless.*/
echo rtrim($res,' ,').'' . '';
}
Extra:
function wt_get_child_terms( $post_id, $taxonomy = 'category', $args = array() ) {
$object_terms = wp_get_object_terms( $post_id, $taxonomy, $args );
$res="";
if ( $object_terms ) {
foreach ( $object_terms as $term ) {
if ( $term->parent ) {
$res .= $term->name . ', ';
}
}
}
return apply_filters( 'wt_get_child_terms', rtrim( $res, ', ' ) );
}
echo wt_get_child_terms( $post->ID, 'types', array( 'fields' => 'all' ) );