cat_is_ancestor_of()

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.0wp-includes/category.php:23201 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; ?>
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索