in_category

函式
in_category ( $category, $post = null )
引數
  • (int|string|int[]|string[]) $category Category ID, name, slug, or array of such to check against.
    Required:
  • (int|WP_Post) $post Optional. Post to check. Defaults to the current post.
    Required:
    Default: null
返回值
  • (bool) True if the current post is in any of the given categories.
定義位置
相關方法
is_categoryget_categoryhas_categorythe_categorywp_insert_category
引入
1.2.0
棄用
-

in_category: 這是一個條件函式,檢查當前文章是否在一個或多個指定的類別中。如果文章是在任何一個指定的類別中,它將返回true,否則返回false。

檢查當前文章是否在任何給定的類別內。

給出的類別會根據文章的類別的 term_ids、名稱和 slugs 進行檢查。以整數形式給出的類別將只與文章的類別的 term_ids 進行檢查。

在WordPress的v2.5版本之前,不支援類別名稱。
在v2.7版之前,不支援分類的片語。
在v2.7版之前,只有一個類別可以被比較:in_category( $single_category )。
在v2.7版之前,這個函式只能在WordPress迴圈中使用。
從2.7版開始,如果提供一個文章ID或文章物件,該函式可以在任何地方使用。

關於這個和類似的主題函式的更多資訊,請檢視《主題開發者手冊》中的{@link Conditional Tags}文章。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function in_category( $category, $post = null ) {
if ( empty( $category ) ) {
return false;
}
return has_category( $category, $post );
}
function in_category( $category, $post = null ) { if ( empty( $category ) ) { return false; } return has_category( $category, $post ); }
function in_category( $category, $post = null ) {
	if ( empty( $category ) ) {
		return false;
	}

	return has_category( $category, $post );
}

常見問題

FAQs
檢視更多 >