wp_get_post_categories

函式
wp_get_post_categories ( $post_id = 0, $args = array() )
引數
  • (int) $post_id Optional. The Post ID. Does not default to the ID of the global $post. Default 0.
    Required:
  • (array) $args Optional. Category query parameters. Default empty array. See WP_Term_Query::__construct() for supported arguments.
    Required:
    Default: array()
返回值
  • (array|WP_Error) List of categories. If the `$fields` argument passed via `$args` is 'all' or 'all_with_object_id', an array of WP_Term objects will be returned. If `$fields` is 'ids', an array of category IDs. If `$fields` is 'names', an array of category names. WP_Error object if 'category' taxonomy doesn't exist.
相關
  • wp_get_object_terms()
定義位置
相關方法
wp_set_post_categorieswp_get_post_catswp_get_post_termswp_list_categorieswp_set_post_cats
引入
2.1.0
棄用
-

wp_get_post_categories: 這個函式檢索一個文章的類別ID陣列。它接受一個文章的ID作為引數。

檢索一個文章的類別列表。

主題和外掛的相容層。也是一個簡單的抽象層,遠離複雜的分類學層。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_get_post_categories( $post_id = 0, $args = array() ) {
$post_id = (int) $post_id;
$defaults = array( 'fields' => 'ids' );
$args = wp_parse_args( $args, $defaults );
$cats = wp_get_object_terms( $post_id, 'category', $args );
return $cats;
}
function wp_get_post_categories( $post_id = 0, $args = array() ) { $post_id = (int) $post_id; $defaults = array( 'fields' => 'ids' ); $args = wp_parse_args( $args, $defaults ); $cats = wp_get_object_terms( $post_id, 'category', $args ); return $cats; }
function wp_get_post_categories( $post_id = 0, $args = array() ) {
	$post_id = (int) $post_id;

	$defaults = array( 'fields' => 'ids' );
	$args     = wp_parse_args( $args, $defaults );

	$cats = wp_get_object_terms( $post_id, 'category', $args );
	return $cats;
}

常見問題

FAQs
檢視更多 >