get_category_link()
Gets the correct link to the category page by the given category ID.
For the function, a special hook category_link is used, which is utilized in the main function for obtaining the URL of terms:
$termlink = apply_filters( 'category_link', $termlink, $term->term_id );
Uses: get_term_link()
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.
Returns
String. A link (URL) to the category page.
Usage
$cat_link = get_category_link( $category_id );
- $category_id(number) (required)
- The ID of the category for which the link needs to be obtained.
Examples
#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
- See: get_term_link()
Changelog
| Since 1.0.0 | Introduced. |
get_category_link() get category link code WP 7.0
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;
}