每个术语对象都有一个parent 属性,如果是父根项,则设置为0;如果是子项,则设置为父项的ID。因此,如果只有一级子术语,则可以检查该属性是否不等于0,在这种情况下,该术语有一个父项。
$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,\' ,\').\'\' . \'\';  
}
 额外费用:
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\' ) );