get_category_parents()WP 1.2.0

Retrieve category parents with separator.

No Hooks.

Return

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

Usage

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

Examples

0

#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 » 
0

#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: https://wp-kama.com/function/get_the_terms#example_257

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

Changelog

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

Code of get_category_parents() WP 6.0.1

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 );
}