Advertisements

get_category_parents

Definition:
function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() {}

Retrieve category parents with separator.

Parameters

  • int $id: Category ID.
  • bool $link: Optional, default is false. Whether to format with link.
  • string $separator: Optional, default is ‘/’. How to separate categories.
  • bool $nicename: Optional, default is false. Whether to use nice name for display.
  • array $visited: Optional. Already linked to categories to prevent duplicates.

Source code

function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {

	$chain = '';

	$parent = &get_category( $id );

	if ( is_wp_error( $parent ) )

		return $parent;



	if ( $nicename )

		$name = $parent->slug;

	else

		$name = $parent->name;



	if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {

		$visited[] = $parent->parent;

		$chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );

	}



	if ( $link )

		$chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;

	else

		$chain .= $name.$separator;

	return $chain;

}

1254

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: