WordPress the_category() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Template tag

Definition

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.

Syntax

Call it with no arguments for a list, or pass a separator such as a comma.

WordPress
the_category();
the_category( ', ' );
the_category( ' | ', '', 42 );
CallWhat 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.

Full declaration
WordPress
the_category( string $separator = '', string $parents = '', int|false $post_id = false ): void

Parameters

ParameterDescription
$separatorOptional. Text placed between the links. If empty, the links are printed as a list.
$parentsOptional. How to show parent categories: multiple, single, or empty. Default is empty.
$post_idOptional. The post ID. Default false, which means the current post.

Return Value

Return ValueDescription
voidNothing is returned. The function prints the category links.

WordPress Examples

Three short theme examples. Each one has a sample output.

Example 1 — Default list

Print the categories as a list.

WordPress
<?php the_category(); ?>

How It Works

  1. With no separator, WordPress builds a ul list.
  2. Each category is one li with a link.
  3. Style .post-categories in your CSS.

Example 2 — Inline links

Print the categories on one line with a label.

WordPress
<p class="cats">Posted in: <?php the_category( ', ' ); ?></p>

How It Works

  1. The first argument ', ' is the separator.
  2. WordPress puts the separator between the links.
  3. The label Posted in: is your own HTML.

Example 3 — Specific post

Print the categories of a post by its ID.

WordPress
<?php the_category( ' | ', '', 42 ); ?>

How It Works

  1. The second argument is empty, so parents are not shown.
  2. The third argument 42 is the post ID.
  3. This lets you print categories outside The Loop.

Frequently Asked Questions

It prints the categories of the current post as links. If the separator is empty, it prints an unordered list.
the_category() prints links. get_the_category() returns an array of category objects that you can loop over.
Pass a post ID as the third argument. Without it, the function needs a current post.
Use get_the_category() and print each $cat->name with esc_html().
Use the_tags() for tags, or the_terms() for any taxonomy.

Next: the_category_rss()

Print the categories of the current post inside an RSS or Atom feed.

the_category_rss() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful