has_term()
云策文档标注
概述
has_term() 函数用于检查当前文章是否包含指定的分类法术语。它支持通过 term_id、名称或 slug 进行匹配,并可指定文章和分类法。
关键要点
- 检查文章是否具有给定术语,支持字符串、整数或数组作为参数。
- 参数包括 $term(可选,术语标识)、$taxonomy(可选,分类法名称)和 $post(可选,文章 ID 或对象)。
- 返回布尔值:如果文章包含任何指定术语(或无术语时检查是否有任何术语),则返回 true;否则返回 false。
- 内部调用 is_object_in_term() 函数,并处理错误情况。
代码示例
// 检查文章是否具有特定术语
if( has_term( 'jazz', 'genre' ) ) {
// 执行操作
}
// 检查文章是否具有任何术语(通过空字符串)
if( has_term('', 'genre') ) {
// 执行操作
}
// 检查自定义文章类型和分类法
if (has_term( 'action', 'hook-type' )) {
$hook_css_class = "is-action-hook";
} else {
$hook_css_class = "is-filter-hook";
}
// 检查多个术语(通过数组)
has_term( array( 'album', 'genre' ), 'product_cat', 23 ) {
// 执行操作
}注意事项
- 术语参数为空时,函数检查文章是否具有任何术语,适用于条件显示内容。
- 支持自定义文章类型和分类法,需确保参数正确匹配。
- 函数从 WordPress 3.1.0 版本引入,相关函数包括 has_category() 和 has_tag()。
原文内容
Checks if the current post has any of given terms.
Description
The given terms are checked against the post’s terms’ term_ids, names and slugs.
Terms given as integers will only be checked against the post’s terms’ term_ids.
If no terms are given, determines if post has any terms.
Parameters
$termstring|int|arrayoptional-
The term name/term_id/slug, or an array of them to check for. Default empty.
$taxonomystringoptional-
Taxonomy name. Default empty.
$postint|WP_Postoptional-
Post to check. Defaults to the current post.
Default:
null
Source
function has_term( $term = '', $taxonomy = '', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$r = is_object_in_term( $post->ID, $taxonomy, $term );
if ( is_wp_error( $r ) ) {
return false;
}
return $r;
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |
Skip to note 6 content
Marc Heatley
If you’re checking for the presence of any terms from a given taxonomy on a post, you can pass in an empty string as the first parameter.
Example
if( has_term('', 'genre') ){ // do something }This is useful if you want to conditionally display some markup that applies only if terms have been added to a post.
Skip to note 7 content
Codex
Example
if( has_term( 'jazz', 'genre' ) ) { // do something }Skip to note 8 content
Oscar Abad Folgueira
Example for check if a post of cpt have a specific term of a custom taxonomy.
In this example we check if the post have a term ‘action’ and in this case asign one css class to a variable that we use later on html.
CPT: “hook”
Taxonomy: “hook-type”
Taxonomy terms: “action” and “filter”.
Note that in this example, this code is inside a cpt archive file “archive-hook.php”.
$hook_css_class = ''; if (has_term( 'action', 'hook-type' )) { $hook_css_class = "is-action-hook"; } else { $hook_css_class = "is-filter-hook"; };Skip to note 9 content
Ahir Hemant
The has_term function in WordPress is used to check if a post has a specific term (category, tag, or custom taxonomy term). Here’s an example:
// Specify the term and taxonomy you want to check $term = 'wpdocs_term'; // Replace with the actual term slug or name $taxonomy = 'wpdocs_category'; // Replace with the actual taxonomy name // Specify the post ID you want to check $post_id = 1; // Replace with the actual post ID // Use has_term to check if the post has the specified term if ( has_term( $term, $taxonomy, $post_id ) ) { // The post has the specified term echo 'Post has the term ' . $term . ' in the ' . $taxonomy . ' taxonomy.'; } else { // The post does not have the specified term echo 'Post does not have the term ' . $term . ' in the ' . $taxonomy . ' taxonomy.'; }Skip to note 10 content
Hasan Fardous
// Check for multiple categories at once by slugs has_term( array( 'album, genre' ), 'product_cat', 23 ) { // do something } // Check for multiple categories at once by IDs has_term( array( '2', '5' ), 'product_cat', 23 ) { // do something }