get_category

函式
get_category ( $category, $output = OBJECT, $filter = 'raw' )
引數
  • (int|object) $category Category ID or category row object.
    Required:
  • (string) $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
    Required:
    Default: OBJECT
  • (string) $filter Optional. How to sanitize category fields. Default 'raw'.
    Required:
    Default: 'raw'
返回值
  • (object|array|WP_Error|null) Category data in type defined by $output parameter. WP_Error if $category is empty, null if it does not exist.
定義位置
相關方法
get_the_categoryget_categoriesthe_categoryget_category_linkis_category
引入
1.5.1
棄用
-

get_category: 這個函式通過ID或slug檢索一個類別物件。它需要一個引數:$category,它是類別的ID或slug。

在給定的類別ID或類別物件中檢索類別資料。

如果你向$category引數傳遞一個物件,它被認為是資料庫中檢索的類別行物件。它將快取類別資料。

如果你傳給$category一個類別ID的整數,那麼該類別將被從資料庫中檢索出來,如果它還沒有被快取的話,並傳回它。

如果你看一下get_term(),那麼這兩種型別都將通過幾個過濾器,最後根據$filter的引數值進行淨化處理。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
$category = get_term( $category, 'category', $output, $filter );
if ( is_wp_error( $category ) ) {
return $category;
}
_make_cat_compat( $category );
return $category;
}
function get_category( $category, $output = OBJECT, $filter = 'raw' ) { $category = get_term( $category, 'category', $output, $filter ); if ( is_wp_error( $category ) ) { return $category; } _make_cat_compat( $category ); return $category; }
function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
	$category = get_term( $category, 'category', $output, $filter );

	if ( is_wp_error( $category ) ) {
		return $category;
	}

	_make_cat_compat( $category );

	return $category;
}

常見問題

FAQs
檢視更多 >