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?
Sounds 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.