get_the_category

Advertisement

Summery Summery

Retrieve post categories.

Syntax Syntax

get_the_category( int $id = false )

Description Description

This tag may be used outside The Loop by passing a post id as the parameter.

Note: This function only returns results from the default "category" taxonomy. For custom taxonomies use get_the_terms().

Parameters Parameters

$id

(Optional) default to current post ID. The post ID.

Default value: false

Return Return

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

Source Source

File: wp-includes/category-template.php

 */
function get_the_category( $post_id = false ) {
	$categories = get_the_terms( $post_id, 'category' );
	if ( ! $categories || is_wp_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 WP_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 );

Advertisement

Changelog Changelog

Changelog
Version Description
0.71 Introduced.

Advertisement

Leave a Reply