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作为参数。

检索一个文章的类别列表。

主题和插件的兼容层。也是一个简单的抽象层,远离复杂的分类学层。

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
查看更多 >