返回介绍

cat_is_ancestor_of()

发布于 2017-09-10 21:38:28 字数 2582 浏览 1240 评论 0 收藏 0

cat_is_ancestor_of( int|object $cat1,  int|object $cat2 )

Check if a category is an ancestor of another category.


description

You can use either an id or the category object for both parameters. If you use an integer the category will be retrieved.


参数

$cat1

(int|object) (Required) ID or object to check if this is the parent category.

$cat2

(int|object) (Required) The child category.


返回值

(bool) Whether $cat2 is child of $cat1


源代码

File: wp-includes/category.php

function cat_is_ancestor_of( $cat1, $cat2 ) {
	return term_is_ancestor_of( $cat1, $cat2, 'category' );
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/taxonomy.php: term_is_ancestor_of()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Basic Example

    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.)

    
    <?php 
      // 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' )); ?>
      </div>
    <?php endif; ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文