WordPress the_category() Function

Definition
In WordPress, the_category() prints the categories of a post as links. Each link goes to the page that lists all posts in that category.
Use it inside The Loop, for example under the post title.
Syntax
A few ways to call the_category().
<?php
the_category( ', ' );
| Call | What it does |
|---|
the_category() | Print the categories as a list. |
the_category( ', ' ) | Print the categories separated by a comma. |
the_category( ' | ', 'multiple' ) | Show parent categories with their children. |
get_the_category_list( ", " ) | Return the same HTML as a string. |
It prints the links. If you pass no separator, the categories are printed as an unordered list.
Full declaration
the_category( string $separator = '', string $parents = '', int|false $post_id = false ): void
Parameters
| Parameter | Description |
|---|
$separator | string. Text between categories. Default is empty, which makes a list. |
$parents | string. How to show parent categories: multiple, single, or empty. Default is empty. |
$post_id | int|false. The post ID. Default is false, which uses the current post. |
Return Value
| Return Value | Description |
|---|
| void | Nothing is returned. The links are printed. |
WordPress Examples
Three short examples. Each one shows the code and what it prints.
Example 1 — Inline category list
Print categories separated by commas.
<p>Posted in: <?php the_category( ', ' ); ?></p>
<p>Posted in: <a href="https://example.com/category/news/" rel="category tag">News</a>, <a href="https://example.com/category/wordpress/" rel="category tag">WordPress</a></p>
How It Works
- The separator
', ' goes between links. - Each link goes to a category archive.
- The
rel="category tag" is added by WordPress.
Example 2 — Default list
With no separator you get an unordered list.
<ul class="post-categories">
<li><a href="https://example.com/category/news/" rel="category tag">News</a></li>
<li><a href="https://example.com/category/wordpress/" rel="category tag">WordPress</a></li>
</ul>
How It Works
- No separator means a list.
- Each category is one
li. - You can style
.post-categories in CSS.
Example 3 — Categories of another post
Pass a post ID as the third argument.
<?php the_category( ' / ', '', 42 ); ?>
<a href="https://example.com/category/news/" rel="category tag">News</a> / <a href="https://example.com/category/tips/" rel="category tag">Tips</a>
How It Works
- The second argument is empty, so there is no parent display.
- The third argument
42 is the post ID. - The separator is
/.
Frequently Asked Questions
It prints the categories of a post as links to their archive pages.
Pass a separator, for example the_category( ', ' ).
With no separator, the_category() prints an unordered list.
Use get_the_category_list() for HTML, or get_the_category() for an array of category objects.
Use the_tags().
Browse all post functions
Open the functions index for simple guides with examples and FAQs.
Post Functions Index →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful