The get_the_category_list() WordPress PHP function retrieves a category list for a post in either HTML list or custom format.
Usage
echo get_the_category_list($separator, $parents, $post_id);
Parameters
- $separator (string) Optional: Separator between the categories. By default, the links are placed in an unordered list. An empty string will result in the default behavior. Default:
''
. - $parents (string) Optional: How to display the parents. Accepts ‘multiple’, ‘single’, or empty. Default:
''
. - $post_id (int) Optional: ID of the post to retrieve categories for. Defaults to the current post. Default:
false
.
More information
See WordPress Developer Resources: get_the_category_list
Examples
Display as List Items
By leaving the $separator value empty, it will generate an unordered list instead, complete with classes.
echo get_the_category_list();
Display Categories Separated by Comma
Display categories separated by a comma and a space.
echo get_the_category_list(', ');
Display Parent Categories Only
Display parent categories only by setting the $parents parameter to 'single'
.
echo get_the_category_list(', ', 'single');
Display Categories for a Specific Post
To display categories for a specific post, provide the $post_id parameter with the post ID.
$post_id = 123; echo get_the_category_list(', ', '', $post_id);
Display Categories with Custom Separator
Display categories separated by a custom separator, such as a pipe symbol (|).
echo get_the_category_list(' | ');