get_category_parents()WP 1.2.0

Retrieve category parents with separator.

No Hooks.


String|WP_Error. A list of category parents on success, WP_Error on failure.


get_category_parents( $category_id, $link, $separator, $nicename, $deprecated );
$category_id(int) (required)
Category ID.
Whether to format with link.
Default: false
How to separate categories.
Default: '/'
Whether to use nice name for display.
Default: false
Not used.
Default: array()



#1 Derive the parent categories of category 10

In the form of links separated by ». The link to category 10 will also be displayed.

<?php echo get_category_parents( 10, true, ' » '); ?>

will output:

Internet » Blogging » WordPress » 

#2 Get the top parent element for the nested element

We need to get the first (root) parent element (term) of the taxonomy to the current element. That is, we have a taxonomy element, and we need to get the topmost element of that term in the hierarchy.

See this example:

See the answer in the question: How do I get the top-level taxonomy element in which the specified post (post) is located?


Since 1.2.0 Introduced.
Since 4.8.0 The $visited parameter was deprecated and renamed to $deprecated.

get_category_parents() code WP 6.3

function get_category_parents( $category_id, $link = false, $separator = '/', $nicename = false, $deprecated = array() ) {

	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __FUNCTION__, '4.8.0' );

	$format = $nicename ? 'slug' : 'name';

	$args = array(
		'separator' => $separator,
		'link'      => $link,
		'format'    => $format,

	return get_term_parents_list( $category_id, 'category', $args );