0

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?

1 Answer 1

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;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.