Example 1 — Default list
Print the categories as a list.
<?php the_category(); ?> How It Works
- With no separator, WordPress builds a
ullist. - Each category is one
liwith a link. - Style
.post-categoriesin your CSS.

In WordPress, the_category() prints the categories of the current post. Each category is a link to its archive page. Use it inside The Loop.
It prints the links. It does not return them. If you leave the separator empty, WordPress prints an unordered list.
Call it with no arguments for a list, or pass a separator such as a comma.
the_category();
the_category( ', ' );
the_category( ' | ', '', 42 ); | Call | What it does |
|---|---|
the_category() | Prints the categories as an unordered list. |
the_category( ', ' ) | Prints the category links on one line, separated by a comma. |
the_category( ' | ', '', 42 ) | Prints the categories of the post with ID 42. |
Use get_the_category() if you need the category objects instead of links.
the_category( string $separator = '', string $parents = '', int|false $post_id = false ): void | Parameter | Description |
|---|---|
$separator | Optional. Text placed between the links. If empty, the links are printed as a list. |
$parents | Optional. How to show parent categories: multiple, single, or empty. Default is empty. |
$post_id | Optional. The post ID. Default false, which means the current post. |
| Return Value | Description |
|---|---|
| void | Nothing is returned. The function prints the category links. |
Three short theme examples. Each one has a sample output.
Print the categories as a list.
<?php the_category(); ?> ul list.li with a link..post-categories in your CSS.Print the categories on one line with a label.
<p class="cats">Posted in: <?php the_category( ', ' ); ?></p> ', ' is the separator.Posted in: is your own HTML.Print the categories of a post by its ID.
<?php the_category( ' | ', '', 42 ); ?> 42 is the post ID.Print the categories of the current post inside an RSS or Atom feed.
9 people found this page helpful