$locations = get_the_terms( $post_id, \'location\' ); 
foreach ( $locations as $location ) {
    echo \'<a href="\'.site_url().\'/location/\'.$location->slug.\'">\'. $location->name .\'</a>\';
    break; 
}
 如果有父分类法,如何获取所有父分类法?
例如:
当前代码输出London 在前端。
需要输出的内容Europe > England > London.
我设法挖掘或尝试的是get_the_terms() 也有parent 可以用作$location->parent. 这个只有ID属性,我并没有设法从中挤出名称或名称。此外,如果它有父对象,也无法使用它来检索它的父对象。
<小时>
Update after some experimenting:
//Parent
if( $location->parent != "" ) {
    $location_parent_id = $location->parent; 
    $location_parent = get_term_by( \'id\', $location_parent_id, \'location\' );
    echo $location_parent->name; //Here it is
}   
//If I need parent of parent
if( $location_parent->parent != "" ) {
    $location_parent_parent_id = $location_parent->parent; 
    $location_parent_parent = get_term_by( \'id\', $location_parent_parent_id, \'location\' );
    echo $location_parent_parent->name; //Here it is
}
//etc
Is this very grazy approach? 
                    最合适的回答,由SO网友:s_ha_dum 整理而成
                    你想要的get_ancestors(). 这很粗糙但是。。。
$locations = get_the_terms( $post->ID, \'category\' ); 
// var_dump($locations);
$pat = \'<a href="\'.site_url().\'/location/%s">%s</a>\';
foreach ( $locations as $location ) {
  printf($pat,$location->slug,$location->name);
  $anc = get_ancestors($location->term_id,\'category\');
  if (!empty($anc)) {
    foreach ($anc as $term) {
      $t = get_term_by( \'term_id\', $term, \'category\');
      printf($pat,$t->slug,$t->name);
    }
  }
  break; 
}
 
                     
                
                
                SO网友:Juan Rangel
                听起来您想以层次结构格式显示分类法?这是我最近做的事情。试试这样的
// Get all taxonomies that do not have a parent/top level taxonomies
$parents = get_terms( $taxonomy, array( \'hide_empty\' => true, \'parent\' => 0 ) );
foreach ( $parents as $parent ) {
    echo $parent->name . \': \';
    $child_terms = array();
    // Get all post terms for this post and use the parent ID to grab the children
    $children = wp_get_post_terms( get_the_ID(), $taxonomy, array( \'hide_empty\' => true, \'parent\' => $parent->term_id ) );
        foreach ( $children as $child ) {
            // build a link to the children taxonomy archive
            $child_terms[] = sprintf( \'<a href="%s">%s</a>\', get_term_link($child, $taxonomy), $child->name );
        }
    echo implode( \', \', $child_terms) . \'<br>\';
}