admin管理员组

文章数量:1315061

I need to check more than one sub-category for has-term() function. For example; Assume that I have 2 categories; Bars and Musicians. Also, the Musicians category has got Jazz, Rock, Pop as a subcategory. But, they are also subcategories for Bars.

I want to write different codes for Bars - Jazz, Musicians - Jazz categories.

This code structure is not working for me.

if( has_term( 'jazz', 'genre' ) ) {
    // do something
}

Because I have jazz subcategory under Bars and Musicians.

When I wrote a code with this structure, it was not working too.

if( has_term( array('musicians','jazz'), 'genre' ) ) {
    // do something
}

How can I solve this issue?

I need to check more than one sub-category for has-term() function. For example; Assume that I have 2 categories; Bars and Musicians. Also, the Musicians category has got Jazz, Rock, Pop as a subcategory. But, they are also subcategories for Bars.

I want to write different codes for Bars - Jazz, Musicians - Jazz categories.

This code structure is not working for me.

if( has_term( 'jazz', 'genre' ) ) {
    // do something
}

Because I have jazz subcategory under Bars and Musicians.

When I wrote a code with this structure, it was not working too.

if( has_term( array('musicians','jazz'), 'genre' ) ) {
    // do something
}

How can I solve this issue?

Share Improve this question asked Nov 1, 2020 at 23:53 083N083N 314 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Reading this solution here: has_category() for parent category

I tried a similar approach and instead of hammering down all the terms I utilized the above solution to leverage the category term or any of its sub-categories:

add_filter('the_content', 'ssws_add_content');
function ssws_add_content($content) {

    $cat_id   = get_cat_ID('accommodations');
    $children = get_term_children($cat_id, 'category');

    $ssws_custom_text = '<h1>Accomodation</h1>';

    if (is_single() && (has_category($cat_id) || has_category($children))) {
        $content .= $ssws_custom_text;
    }

    return $content;

}

本文标签: theme developmentUsing hasterm() function for categorysubcategory structure