Function Name: get_category_link
If you’re building a WordPress website and you need to display a link to a specific category page, you should use the get_category_link() function. This function retrieves the URL for a category archive link.
This function accepts a single parameter – the category ID or category object – and returns the URL for the category archive page, which displays all the posts in that category.
Here’s an example usage code:
$category_id = 5; // ID of the category you want to link to
$category_link = get_category_link( $category_id );
echo '<a href="' . esc_url( $category_link ) . '">Category Name</a>';
In this example, we’re getting the link for the category with an ID of 5, and then using it to create an HTML link with the text "Category Name". The esc_url() function is used to sanitize the URL for output to make sure it’s safe and compliant with HTML standards.
Overall, get_category_link() is a simple but useful function for WordPress developers who want to create links to specific category pages.