返回介绍

term_is_ancestor_of()

发布于 2017-09-11 10:30:55 字数 3467 浏览 1035 评论 0 收藏 0

term_is_ancestor_of( int|object $term1,  int|object $term2,  string $taxonomy )

Check if a term is an ancestor of another term.


description

You can use either an id or the term object for both parameters.


参数

$term1

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

$term2

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

$taxonomy

(string) (Required) Taxonomy name that $term1 and $term2 belong to.


返回值

(bool) Whether $term2 is a child of $term1.


源代码

File: wp-includes/taxonomy.php

function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
	if ( ! isset( $term1->term_id ) )
		$term1 = get_term( $term1, $taxonomy );
	if ( ! isset( $term2->parent ) )
		$term2 = get_term( $term2, $taxonomy );

	if ( empty( $term1->term_id ) || empty( $term2->parent ) )
		return false;
	if ( $term2->parent == $term1->term_id )
		return true;

	return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );
}

更新日志

Versiondescription
3.4.0Introduced.

相关函数

Uses

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

Used By

  • wp-admin/includes/taxonomy.php: wp_insert_category()
  • wp-includes/category.php: cat_is_ancestor_of()
  • 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

    Use conditional tag to show different content based on term being displayed.

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

    The code snip below checks to see if the term called ‘Music’ (ID 4) for the taxonmy ‘Sound’ is being processed, and if so, presents a wp_nav_menu for the Music archive page, and any subterms of Music (e.g. jazz, classical.)

    
    <?php
    // If the taxonomy is sound and the term is music.
    if ( term_is_ancestor_of( 4, $term, 'sound' ) || is_term( 4, 'sound' ) ) : ?>
      <div id="music_subnav_menu" class="subnav_menu">
        <?php wp_nav_menu( array( 'menu' => 'Music' ) ); ?>
      </div>
    <?php endif; ?>
    

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

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

发布评论

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