has_term

函式
has_term ( $term = '', $taxonomy = '', $post = null )
引數
  • (string|int|array) $term Optional. The term name/term_id/slug, or an array of them to check for. Default empty.
    Required:
    Default: (empty)
  • (string) $taxonomy Optional. Taxonomy name. Default empty.
    Required:
    Default: (empty)
  • (int|WP_Post) $post Optional. Post to check. Defaults to the current post.
    Required:
    Default: null
返回值
  • (bool) True if the current post has any of the given terms (or any term, if no term specified). False otherwise.
定義位置
相關方法
is_termhas_filterhas_term_metathe_termshas_category
引入
3.1.0
棄用
-

has_term – 這是一個WordPress函式,用來檢查是否有一個特定的分類術語被分配給當前的文章。分類法是對網站內容進行分組的一種方式,而術語是分類法中的單個類別或標籤。has_term函式需要兩個引數:要檢查的術語和分類法的名稱。如果當前文章在指定的分類法中具有指定的術語,則返回true;如果沒有,則返回false。

檢查當前文章是否有任何給定的術語。

給定的術語將與文章的術語的 term_ids、名稱和 slugs 進行檢查。以整數形式給出的術語將只與文章中的術語的 term_ids 進行檢查。

如果沒有給出術語,則確定文章是否有任何術語。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >