Wordpress 检查当前帖子是否在父类别内,或者它是';s子类别
我有200多个类别,我需要能够检查当前类别(在category.php上)是否在父类别数组中 我读过很多方法来检查帖子是否属于某个类别或子类别,或者一只猫是否是另一只猫的孩子或父母。但是我找不到任何关于检查当前cat是否在父类别数组中的WP函数或问题 以下是我试图实现的目标:Wordpress 检查当前帖子是否在父类别内,或者它是';s子类别,wordpress,function,categories,archive,Wordpress,Function,Categories,Archive,我有200多个类别,我需要能够检查当前类别(在category.php上)是否在父类别数组中 我读过很多方法来检查帖子是否属于某个类别或子类别,或者一只猫是否是另一只猫的孩子或父母。但是我找不到任何关于检查当前cat是否在父类别数组中的WP函数或问题 以下是我试图实现的目标: 父1(ID 1) 儿童1(身份证5) 儿童2(身份证6) 儿童3(身份证7) 孙女1(身份证号码8) 家长2(ID 2) 家长3(ID 3) 家长4(ID 4) 我需要这样做: IF( cat_is_chi
- 父1(ID 1)
- 儿童1(身份证5)
- 儿童2(身份证6)
- 儿童3(身份证7)
- 孙女1(身份证号码8)
- 家长2(ID 2)
- 家长3(ID 3)
- 家长4(ID 4)
IF( cat_is_child_of( array(1,2,3,4)) ):
Do something amazing
ELSE
The current category is NOT within any of the parent categories for the ID's given
ENDIF
有什么想法吗
<?php if (
is_post_type_archive( 'inventory' )
|| get_post_type( get_the_ID() ) == 'inventory'
// check if category IS one of these parent cats
|| is_category(array( 25,28,124,297,298,299 ))
// ugly version of checking if this cat is SUB of any parent cats
|| cat_is_ancestor_of(25, get_query_var( 'cat' ))
|| cat_is_ancestor_of(124, get_query_var( 'cat' ))
|| cat_is_ancestor_of(297, get_query_var( 'cat' ))
|| cat_is_ancestor_of(298, get_query_var( 'cat' ))
|| cat_is_ancestor_of(299, get_query_var( 'cat' ))
):
?>
经过大量的搜索,只找到了检查类别是否有子类别的函数,当然,一篇文章是否(仅)在子类别中,我发现一篇旧文章有一个功能:
if ( ! function_exists( 'post_is_in_a_subcategory' ) ) {
function post_is_in_a_subcategory( $categories, $_post = null ) {
foreach ( (array) $categories as $category ) {
// get_term_children() only accepts integer ID
$subcats = get_term_children( (int) $category, 'category' );
if ( $subcats && in_category( $subcats, $_post ) )
return true;
}
return false;
}
}
以及我如何使用它:
<?php if (
is_post_type_archive( 'inventory' )
|| get_post_type( get_the_ID() ) == 'inventory'
// in one of the parent cats?
|| is_category(array( 25,28,124,297,298,299 ))
// in ANY of the subcats?
|| post_is_in_a_subcategory( array( 25,28,124,297,298,299 ) )
):
?>
例如,最初的功能在Wordpress codex中,但不在核心中。它已经被移除了