我试图在WordPress中引入父分类法描述,但似乎有点困难。。。这似乎只是拉入第一个父类别描述的类别,而不是拉入它所属父类别的描述。。。
<?php
$terms = get_the_terms( $post->ID, \'service\' );
if($terms) {
foreach( $terms as $term ) {
$colour_scheme = get_field(\'colour_scheme\', $term);
$svg_image = get_field(\'svg_image\', $term);
$term = get_term_by("id", $term->parent, "service");
$cat_obj = get_term($term->term_id, \'service\');
$cat_slug = $cat_obj->slug;
$cat_desc = $cat_obj->description;
}
有人知道是什么导致了这个问题吗?
SO网友:djboris
听起来很奇怪,但是你能检查一下这个代码是否有效吗:
$terms = get_the_terms( $post->ID, \'service\' );
if ( $terms ) {
foreach ( $terms as $term ) {
$colour_scheme = get_field( \'colour_scheme\', $term );
$svg_image = get_field( \'svg_image\', $term );
// we check if the term is top-level term, in which case it does not have a parent
$parent = ( $term->parent == 0 ) ? $term : get_term( $term->parent, \'service\' );
$parent_slug = $parent->slug;
$parent_desc = $parent->description;
}
}
我打电话给
get_term()
直接,无需使用
get_term_by()
也