WordPress get parent category taxonomy

admin2025-06-07  27

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 1
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749258163a317675.html

最新回复(0)