get_the_category_by_id

函式
get_the_category_by_id ( $cat_id )
引數
  • (int) $cat_id Category ID.
    Required:
返回值
  • (string|WP_Error) Category name on success, WP_Error on failure.
定義位置
相關方法
get_the_category_listget_the_categoryget_the_category_rssthe_category_idget_the_author_id
引入
0.71
棄用
-

get_the_category_by_id: 這個函式檢索一個特定文章ID的類別物件陣列。它需要一個引數:文章的ID。它返回一個WP_Term物件的陣列。

根據類別ID檢索類別名稱。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_the_category_by_ID( $cat_id ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
$cat_id = (int) $cat_id;
$category = get_term( $cat_id );
if ( is_wp_error( $category ) ) {
return $category;
}
return ( $category ) ? $category->name : '';
}
function get_the_category_by_ID( $cat_id ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid $cat_id = (int) $cat_id; $category = get_term( $cat_id ); if ( is_wp_error( $category ) ) { return $category; } return ( $category ) ? $category->name : ''; }
function get_the_category_by_ID( $cat_id ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
	$cat_id   = (int) $cat_id;
	$category = get_term( $cat_id );

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

	return ( $category ) ? $category->name : '';
}

常見問題

FAQs
檢視更多 >