admin管理员组文章数量:1487745
I'm trying to pull in the parent taxonomy description in WordPress, but seem to be having a little bit of difficulty... this only seems to be pulling in the category of the description of the very first parent category rather than pulling in the description of the parent category it belongs to...
<?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;
}
Anyone have any ideas as to what might be causing this issue?
I'm trying to pull in the parent taxonomy description in WordPress, but seem to be having a little bit of difficulty... this only seems to be pulling in the category of the description of the very first parent category rather than pulling in the description of the parent category it belongs to...
<?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;
}
Anyone have any ideas as to what might be causing this issue?
Share Improve this question edited Oct 16, 2018 at 13:43 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Oct 16, 2018 at 12:41 StuartStuart 11 Answer
Reset to default 0Sounds weird, but can you check if this code works:
$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;
}
}
I called the get_term()
directly, no need to fetch the term twice using get_term_by()
as well.
本文标签: WordPress get parent category taxonomy
版权声明:本文标题:WordPress get parent category taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1749258120a2925590.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论