get_the_category()

最后更新于:2021-11-27 01:39:42

get_the_category( int$post_id=false)

Retrieves post categories.

参数

$post_id

(int) (Optional) The post ID. Defaults to current post ID.

Default value: false

响应

(GC_Term[]) Array of GC_Term objects, one for each category assigned to the post.

源文件

文件: gc-includes/category-template.php

function get_the_category( $post_id = false ) {
	$categories = get_the_terms( $post_id, 'category' );
	if ( ! $categories || is_gc_error( $categories ) ) {
		$categories = array();
	}

	$categories = array_values( $categories );

	foreach ( array_keys( $categories ) as $key ) {
		_make_cat_compat( $categories[ $key ] );
	}

	/**
	 * Filters the array of categories to return for a post.
	 *
	 * @since 3.1.0
	 * @since 4.4.0 Added `$post_id` parameter.
	 *
	 * @param GC_Term[] $categories An array of categories to return for the post.
	 * @param int|false $post_id    ID of the post.
	 */
	return apply_filters( 'get_the_categories', $categories, $post_id );
}
array(1) {
[0]=>
  object(stdClass)#310 (17) {
    ["term_id"]=>
    &int(6)
    ["name"]=>
    &string(10) "familylife"
    ["slug"]=>
    &string(10) "familylife"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(6)
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    &string(0) ""
    ["parent"]=>
    &int(0)
    ["count"]=>
    &int(208)
    ["object_id"]=>
    int(7729)
    ["filter"]=>
    string(3) "raw"
    ["cat_ID"]=>
    &int(6)
    ["category_count"]=>
    &int(208)
    ["category_description"]=>
    &string(0) ""
    ["cat_name"]=>
    &string(10) "familylife"
    ["category_nicename"]=>
    &string(10) "familylife"
    ["category_parent"]=>
    &int(0)
  }
}