term_is_ancestor_of

函数
term_is_ancestor_of ( $term1, $term2, $taxonomy )
参数
  • (int|object) $term1 ID or object to check if this is the parent term.
    Required:
  • (int|object) $term2 The child term.
    Required:
  • (string) $taxonomy Taxonomy name that $term1 and `$term2` belong to.
    Required:
返回值
  • (bool) Whether `$term2` is a child of `$term1`.
定义位置
相关方法
cat_is_ancestor_ofget_post_ancestorsget_ancestors_get_post_ancestorspermalink_anchor
引入
3.4.0
弃用
-

term_is_ancestor_of: 这个函数检查一个术语是否是另一个术语的祖先。

检查一个术语是否是另一个术语的祖先。

你可以使用ID或术语对象作为两个参数。

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 );
}

常见问题

FAQs
查看更多 >
闪电侠

(工作日 10:00 - 18:30 为您服务)

2026-01-29 19:09:56

您好,无论是售前、售后、意见建议……均可通过联系工单与我们取得联系。

您也可选择聊天工具与我们即时沟通或点击查看:

您的工单我们已经收到,我们将会尽快跟您联系!
取消
选择聊天工具: