cat_is_ancestor_of()函数是Wordpress函数,检查一个类别是否是另一个类别的祖先。
cat_is_ancestor_of( int|object $cat1, int|object $cat2 )
说明(Description)
两个参数都可以使用id或category对象。如果使用整数,则将检索类别。
参数(Parameters)
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
$cat1 | (int | object) | 必需 | 要检查这是否是父类别的ID或对象。 |
$cat2 | (int | object) | 必需 | 子类别。 |
返回(Return)
(bool)是否$cat2是$cat1的子级
源码(Source)
/**
* Check if a category is an ancestor of another category.
*
* You can use either an id or the category object for both parameters. If you
* use an integer the category will be retrieved.
*
* @since 2.1.0
*
* @param int|object $cat1 ID or object to check if this is the parent category.
* @param int|object $cat2 The child category.
* @return bool Whether $cat2 is child of $cat1
*/
function cat_is_ancestor_of( $cat1, $cat2 ) {
return term_is_ancestor_of( $cat1, $cat2, 'category' );
}
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
2.1.0 | wp-includes/category.php:232 | 0 | 1 function |
笔记(Notes)
基本示例:以下示例判断当前分类是否ID为4的分类的子分类,或是否为ID为4的分类。
<?php if (cat_is_ancestor_of(4, $cat) or is_category(4)) : ?>
<div id="music_subnav_menu" class="subnav_menu">
<?php wp_nav_menu( array('menu' => 'Music' )); ?>
</div>
<?php endif; ?>