返回介绍

is_object_in_term()

发布于 2017-09-11 01:21:05 字数 4101 浏览 930 评论 0 收藏 0

is_object_in_term( int $object_id,  string $taxonomy,  int|string|array $terms = null )

Determine if the given object is associated with any of the given terms.


description

The given terms are checked against the object’s terms’ term_ids, names and slugs. Terms given as integers will only be checked against the object’s terms’ term_ids. If no terms are given, determines if object is associated with any terms in the given taxonomy.


参数

$object_id

(int) (Required) ID of the object (post ID, link ID, ...).

$taxonomy

(string) (Required) Single taxonomy name.

$terms

(int|string|array) (Optional) Term term_id, name, slug or array of said.

Default value: null


返回值

(bool|WP_Error) WP_Error on input error.


源代码

File: wp-includes/taxonomy.php

function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
	if ( !$object_id = (int) $object_id )
		return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );

	$object_terms = get_object_term_cache( $object_id, $taxonomy );
	if ( false === $object_terms ) {
		$object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
		if ( is_wp_error( $object_terms ) ) {
			return $object_terms;
		}

		wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" );
	}

	if ( is_wp_error( $object_terms ) )
		return $object_terms;
	if ( empty( $object_terms ) )
		return false;
	if ( empty( $terms ) )
		return ( !empty( $object_terms ) );

	$terms = (array) $terms;

	if ( $ints = array_filter( $terms, 'is_int' ) )
		$strs = array_diff( $terms, $ints );
	else
		$strs =& $terms;

	foreach ( $object_terms as $object_term ) {
		// If term is an int, check against term_ids only.
		if ( $ints && in_array( $object_term->term_id, $ints ) ) {
			return true;
		}

		if ( $strs ) {
			// Only check numeric strings against term_id, to avoid false matches due to type juggling.
			$numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) );
			if ( in_array( $object_term->term_id, $numeric_strs, true ) ) {
				return true;
			}

			if ( in_array( $object_term->name, $strs ) ) return true;
			if ( in_array( $object_term->slug, $strs ) ) return true;
		}
	}

	return false;
}

更新日志

Versiondescription
2.7.0Introduced.

相关函数

Uses

  • wp-includes/cache.php: wp_cache_set()
  • wp-includes/l10n.php: __()
  • wp-includes/functions.php: wp_list_pluck()
  • wp-includes/taxonomy.php: get_object_term_cache()
  • wp-includes/taxonomy.php: wp_get_object_terms()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 2 more uses Hide more uses

Used By

  • wp-includes/category-template.php: has_term()
  • wp-includes/nav-menu.php: wp_update_nav_menu_item()

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

    Example

    
    <?php
    if ( is_object_in_term( $post->ID, 'custom_taxonomy_name', 'term_name' ) ) :
    	echo 'YES';
    else :
    	echo 'NO';
    endif;
    ?>
    

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

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

发布评论

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