WordPress at a glance

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).

Works based on: get_the_category_list()
✈ 1 time = 0.006019s = very slow | 50000 times = 16.46s = slow | PHP 7.1.2, WP 4.7.3

No Hooks.

Return

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

#1 Comma separated list of post categories:

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

Result: Categories: WordPress, Computers, Blogging

#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

#3 Set 'single' in the second parameter

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

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

Code of the category: wp-includes/category-template.php WP 5.2.1

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

Related Functions

From category: For posts

More from Template Tags: Posts, Pages, ...

No comments
    Hello, !     Log In . Register