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 badges1 Answer
Reset to default 0Reading 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
版权声明:本文标题:theme development - Using has_term() function for category-subcategory structure 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971554a2407870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论