函数原型:
term_exists( int|string $term, string $taxonomy = ”, int $parent_term = null ): mixed
检查类别是否存在。
参数说明:
$term要检查的分类。接受分类ID、slug或名称。
$taxonomy要使用的分类名称。默认值:“”
$parent_term用于限制现有搜索的父项的ID。默认值:null
返回值:
如果分类不存在,则返回null。
如果未指定分类法且分类ID存在,则返回分类ID。
如果指定了分类法并且存在配对,则返回分类ID和分类分类法ID的数组。
如果将项ID 0传递给函数,则返回0。
函数源码:
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;
}
包含钩子:
apply_filters( ‘term_exists_default_query_args’, array $defaults, int|string $term, string $taxonomy, int|null $parent_term )
使用举例:
$term = term_exists( 'Uncategorized', 'category' );
if ( $term !== 0 && $term !== null ) {
echo __( "'Uncategorized' category exists!", "textdomain" );
}
-
WP Multilang WordPress翻译插件WP Multilang插件的主要功能是提供多语言支持,使用户能够创建多语言版本的网站,满足不同语言用户的需求。
-
Post Type Switcher将 WordPress文章从一个类型转换到另一个类型Post Type Switcher是一个WordPress插件,用于将文章从一个类型转换到另一个类型,包括自定义文章类型。安装并启用该插件后,在编辑文章时,会在发布面板中增加Post Type选项,用户可以选择要转换到的文章类型并更新文章。此外,插件也支持在后台所有文章页面批量更改文章类型。
-
WordPress公司官网主题在众多的WordPress企业官网主题中,追格公司推出了多款关于WordPress企业官网主题作品。我们之前已经分享过一些关于追格的企业官网主题,包括收费和免费版本,这些主题都因其独特的设计和出色的功能而深受用户喜爱。
-
Fluent Forms - WordPress表单插件Fluent Forms是一款功能强大且易于使用的WordPress表单插件,适合各种规模的网站使用。无论是简单的联系表单还是复杂的用户调查表,Fluent Forms都能满足用户的需求。
-
WP-Ban WordPress禁止指定IP访问网站的插件WP-Ban是一款专为WordPress网站设计的安全插件,其主要功能是屏蔽恶意或不受欢迎的IP地址,以增强网站的安全性。
-
WPvivid插件:WordPress网站备份与还原的解决方案WPvivid插件是一款功能强大的WordPress插件,专为网站数据备份、还原和搬家设计。它支持高度自定义的备份选项,允许用户选择备份整个站点(包括数据库和文件)、仅文件或仅数据库。
暂无评论,抢个沙发...