cat_is_ancestor_of() 方法用于判断某个分类是否是另一个分类的子类,例如  的代码 示:若类别1是类 2的上级,函数返回true。只要是上 ,无论多少 级,一律返回true。变量可以是整数类别编号或类别对象。

/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */
<?php cat_is_ancestor_of(cat1, cat2); ?> 

注意 量是整数的 符串形式而不是真正的整数,cat_is_ancestor_of返回false。

cat_is_ancestor_of() 描述


 <?php cat_is_ancestor_of$cat1$cat2 ); ?> 

cat_is_ancestor_of() 用法



(int/object) (必填) ID or object to check if this is the parent category.

默认值: None


(int/object) (必填) The child category.

默认值: None

cat_is_ancestor_of() 参数



True if cat1 is an ancestor of cat2, False if not.

cat_is_ancestor_of() 返回值


This example, placed in a theme's archive.php, uses Conditional Tags to show different content depending on the category being displayed. This is helpful when it is necessary to include something for any child category of a given category, instead of using category-slug.php method where you'd have to create category-slug.php files for each and every category.

The code snip below checks to see if the category called 'Music' (ID 4) is being processed, and if so, presents a wp_nav_menu for the Music archive page, and any subcategories of Music (e.g. jazz, classical.)

/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */

  // if the category is music or a music SUBcategory, 
  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' )); ?>
<?php endif; ?>

cat_is_ancestor_of() 示例


  • The function evaluates if the second category is a child of the first category.
  • Any level of ancestry will return True.
  • Arguments should be either integer or objects, If arguments are string representations of integers and not true integers cat_is_ancestor_of will return False.

cat_is_ancestor_of() 注意


添加于 版本: 2.1.0

cat_is_ancestor_of() 历史


cat_is_ancestor_of() 函数的代码位于 wp-includes/category.php.

/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */
 * 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' );

cat_is_ancestor_of() 源文件