WordPress函数term_exists()检查分类元素是否存在

WordPress函数term_exists()检查分类元素是否存在

WordPress函数term_exists()检查分类元素是否存在

  WordPress 中的函数term_exists()检查给定分类中是否存在特定术语。它需要三个参数:$term$taxonomy$parent_term

  推荐:[最新版]WordPress问答插件免费下载DW Question Answer Pro插件

term_exists()函数基本语法

描述

  检查分类元素是否存在

用法

term_exists ( $term , $taxonomy , $parent_term );

参数

  • $term (string|int)(必需)您要检查的术语。您可以指定名称、备用名称(slug)或 ID。如果指定了 ID,则该值必须是“数字”类型,但不能是“字符串”类型的数字。例如12,不是'12'
  • $分类法(字符串)该函数将使用的分类法的名称。当 $term 参数中传递 term ID 时,不需要指定它。默认: ”
  • $parent (字符串|整数)应在其下搜索指定分类项的父术语的 ID。默认值:空

  推荐:[最新版]Kadence Theme Pro插件下载多用途轻量级WordPress主题

term_exists()函数

  WordPress 中的term_exists()函数函数常用于在插入/更新分类法项前检查其是否已存在,以避免创建重复的分类法项。也可以根据返回的ID来获取更多关于这个分类法项的信息。(源文件可参考这里

function term_exists( $term, $taxonomy = '', $parent_term = null ) {
	global $_wp_suspend_cache_invalidation;

	if ( null === $term ) {
		return null;
	}

	$defaults = array(
		'get'                    => 'all',
		'fields'                 => 'ids',
		'number'                 => 1,
		'update_term_meta_cache' => false,
		'order'                  => 'ASC',
		'orderby'                => 'term_id',
		'suppress_filter'        => true,
	);

	// Ensure that while importing, queries are not cached.
	if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
		$defaults['cache_results'] = false;
	}

	if ( ! empty( $taxonomy ) ) {
		$defaults['taxonomy'] = $taxonomy;
		$defaults['fields']   = 'all';
	}

	/**
	 * Filters default query arguments for checking if a term exists.
	 *
	 * @since 6.0.0
	 *
	 * @param array      $defaults    An array of arguments passed to get_terms().
	 * @param int|string $term        The term to check. Accepts term ID, slug, or name.
	 * @param string     $taxonomy    The taxonomy name to use. An empty string indicates
	 *                                the search is against all taxonomies.
	 * @param int|null   $parent_term ID of parent term under which to confine the exists search.
	 *                                Null indicates the search is unconfined.
	 */
	$defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent_term );

	if ( is_int( $term ) ) {
		if ( 0 === $term ) {
			return 0;
		}
		$args  = wp_parse_args( array( 'include' => array( $term ) ), $defaults );
		$terms = get_terms( $args );
	} else {
		$term = trim( wp_unslash( $term ) );
		if ( '' === $term ) {
			return null;
		}

		if ( ! empty( $taxonomy ) && is_numeric( $parent_term ) ) {
			$defaults['parent'] = (int) $parent_term;
		}

		$args  = wp_parse_args( array( 'slug' => sanitize_title( $term ) ), $defaults );
		$terms = get_terms( $args );
		if ( empty( $terms ) || is_wp_error( $terms ) ) {
			$args  = wp_parse_args( array( 'name' => $term ), $defaults );
			$terms = get_terms( $args );
		}
	}

	if ( empty( $terms ) || is_wp_error( $terms ) ) {
		return null;
	}

	$_term = array_shift( $terms );

	if ( ! empty( $taxonomy ) ) {
		return array(
			'term_id'          => (string) $_term->term_id,
			'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
		);
	}

	return (string) $_term;
}

  推荐:NEX-Forms插件下载WordPress表单生成器插件+ Addons

如何使用term_exists()

  检查术语是否存在(在任何分类中)

$term = term_exists( 'miscellaneous' );
//returns the ID of the taxonomy element with the 'miscellaneous' slug

$term = term_exists( 'Men Clothes' );
//returns the ID of the taxonomy element named 'Men Clothes'

  检查特定分类中是否存在术语,检查my_tax分类术语是否存在:

$term = term_exists( 'Men clothing', 'my_tax' );
//returns an array
//    [term_id] => 80
//    [term_taxonomy_id] => 84

// taxonomy element ID
echo $term['term_id'];

// taxonomy element ID in taxonomy structure
echo $term['term_taxonomy_id'];

  检查Uncategorized类别是否存在:

$term = term_exists( 'Uncategorized', 'category' );

if ( $term !== 0 && $term !== null ) {
	echo 'Uncategorized category exists!';
}

  推荐:WordPress函数get_theme_support()获取注册支持时传递的主题参数

  推荐:WordPress函数使用手册

5/5 - (1 vote)

晓得博客,版权所有丨如未注明,均为原创
晓得博客 » WordPress函数term_exists()检查分类元素是否存在

转载请保留链接:https://www.pythonthree.com/wordpress-term-exists/

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部