The get_category_parents() WordPress PHP function retrieves the category parents of a given category with a specified separator.
Usage
get_category_parents($category_id, $link = false, $separator = '/', $nicename = false, $deprecated = array())
Custom example:
echo get_category_parents(3, true, ' > ');
Output:
Web Development > PHP > Laravel
Parameters
$category_id
(int) – Required. The ID of the category.$link
(bool) – Optional. Whether to format with a link. Default isfalse
.$separator
(string) – Optional. How to separate categories. Default is'/'
.$nicename
(bool) – Optional. Whether to use the nice name (slug) for display. Default isfalse
.$deprecated
(array) – Optional. Not used. Default isarray()
.
More information
See WordPress Developer Resources: get_category_parents()
Note: The $nicename
parameter refers to the slug, not the human-readable display name.
Examples
Basic usage
This code retrieves the parent categories of the current category without links, separated by /
.
echo get_category_parents($cat);
With links
This code retrieves the parent categories of the current category with links, separated by >
.
echo get_category_parents($cat, true, ' > ');
Using slug for display
This code retrieves the parent categories of the current category using the slug for display.
echo get_category_parents($cat, false, '/', true);
Custom separator and links
This code retrieves the parent categories of a specific category with links, separated by |
.
echo get_category_parents(5, true, ' | ');
Retrieving parent categories for a custom category ID
This code retrieves the parent categories of a custom category ID without links, separated by /
.
echo get_category_parents(12, false, ' / ');