get_the_category()WP 0.71

Gets an array of categories data related to current post.

The function can be used outside of the WordPress loop by passing the post ID.

Use get_the_terms(), to get terms from another taxonomy (not "category").

Hooks from the function

Return

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

Each WP_Term object is also augmented with properties generated by _make_cat_compat().

Usage

get_the_category( $post_id );
$post_id(int)
The post ID.
Default: current post ID

Examples

#1 Function Response Example

$categories = get_the_category();

print_r( $categories );

Result:

Array
(
	[0] => WP_Term Object
		(
			[term_id] => 7
			[name] => Codex
			[slug] => codex
			[term_group] => 0
			[term_taxonomy_id] => 7
			[taxonomy] => category
			[description] =>
			[parent] => 3
			[count] => 17
			[filter] => raw
			[term_order] => 1
			[cat_ID] => 7
			[category_count] => 17
			[category_description] =>
			[cat_name] => Codex
			[category_nicename] => codex
			[category_parent] => 3
		)

	[1] => WP_Term Object
		(
			[term_id] => 4
			[name] => Usefulness
			[slug] => usefulness
			[term_group] => 0
			[term_taxonomy_id] => 4
			[taxonomy] => category
			[description] =>
			[parent] => 3
			[count] => 7
			[filter] => raw
			[term_order] => 2
			[cat_ID] => 4
			[category_count] => 7
			[category_description] =>
			[cat_name] => Usefulness
			[category_nicename] => usefulness
			[category_parent] => 3
		)

)

#2 Get first category data from the array

Show the First Category Name Only.

In this example we get first array element ([0]) of $categories.

$categories = get_the_category();

if ( ! empty( $categories ) ) {
	echo esc_html( $categories[0]->name );
}

Make the first category link to the category page:

$categories = get_the_category();

if ( ! empty( $categories ) ) {
	echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
}

#3 First category name

Show only the name of the first category (if the post belongs to one or more categories):

$cats = get_the_category();
echo array_shift( $cats )->name;

#3 Post categories outside the loop

An example of how to get post categories outside the WordPress Loop:

global $post;

$categories = get_the_category( $post->ID );

The data that each category object contains (see all properties in class WP_Term and function _make_cat_compat()):

$cat = get_the_category($post->ID);

//category ID
$cat->term_id

//category name
$cat->name

// Category Label
$cat->slug

// Category description (set on the category editing page)
$cat->description

// ID of parent category
$cat->parent

// Number of records in the category
$cat->count

#4 Output links to the post's categories

foreach ( get_the_category() as $category ) {
	printf(
		'<a href="%s" class="link link-text">%s</a>',
		esc_url( get_category_link( $category ) ),
		esc_html( $category->name )
	);
}

#5 Comma-separated links to the post's categories

$links = array_map(
	function ( $category ) {
		return sprintf(
			'<a href="%s" class="link link-text">%s</a>',
			esc_url( get_category_link( $category ) ),
			esc_html( $category->name )
		);
	},
	get_the_category()
);

echo implode( ', ', $links );

#6 Let's show an image for each category

This example shows how you can create an image for each category, the alt attribute will show the name of the category. The images must have the same names as the category IDs (12.jpg) and must be located in the root of the site in the images folder. The code must be used inside the WP Loop:

foreach( get_the_category() as $category ){
	echo '<img src="http://example.com/images/' . $category->term_id . '.jpg" alt="' . $category->name . '" />';
}

Changelog

Since 0.71 Introduced.

Code of get_the_category() WP 6.0

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 );
}