get_category_link()WP 1.0.0

Retrieve category link URL.

Used By: get_tag_link()
1 time — 0.0038219 sec (very slow) | 50000 times — 3.29 sec (fast) | PHP 7.4.25, WP 5.9.2

No Hooks.

Return

String. Link on success, empty string if category does not exist.

Usage

get_category_link( $category );
$category(int|object) (required)
Category ID or object.

Examples

0

#1 Display a link to the 'Pets' category:

<?php
// Get the category ID
$category_id = get_cat_ID( 'Pets' );

// Now, let's get a category URL
$category_link = get_category_link( $category_id );
?>

<!-- bring up a link to the category -->
<a href="<?php echo $category_link; ?>">Pets</a>

Notes

Changelog

Since 1.0.0 Introduced.

get_category_link() code WP 6.5.2

function get_category_link( $category ) {
	if ( ! is_object( $category ) ) {
		$category = (int) $category;
	}

	$category = get_term_link( $category );

	if ( is_wp_error( $category ) ) {
		return '';
	}

	return $category;
}