the_category()WP 0.71

Displays links to the categories to which the post belongs in either HTML list or custom format.

The function should be used inside the WordPress Loop or the $post_id parameter should be passed (see description).

1 time — 0.006019 sec (very slow) | 50000 times — 16.46 sec (slow) | PHP 7.1.2, WP 4.7.3

No Hooks.

Return

null. Nothing (null).

Usage

the_category( $separator, $parents, $post_id );
$separator(string)
Separator between the links.
Default: '' - displays links in the <ul> list
$parents(string)

How to show links if the current category is a child category. Can be:

  • multiple - show links to parent and child categories separately, keeping order (parent/ancestor).

  • single - show one link to the category in which the post is located, but the whole category (parent/ancestor) nesting structure becomes the anchor of the link.

Default: '' - just displays the categories to which the post belongs (no relation is taken into account)

$post_id(int)
ID of the post whose categories you want to display.
Default: false - current post ($post->ID)

Examples

0

#1 Comma separated list of post categories:

<p>Categories: <?php the_category(', '); ?></p>

Result: Categories: WordPress, Computers, Blogging

0

#2 Use the second parameter

Suppose that the post is in category "Pushkin", which is the sub-category of "Poets", then:

<?php the_category(' > ', 'multiple'); ?>

Displays: Poets > Pushkin

-1

#3 Set 'single' in the second parameter

<?php the_category(' > ', 'single'); ?>

Result: Poets > Pushkin (link will lead to the category "Pushkin")

Changelog

Since 0.71 Introduced.

the_category() code WP 6.4.3

function the_category( $separator = '', $parents = '', $post_id = false ) {
	echo get_the_category_list( $separator, $parents, $post_id );
}