get_term_link

Home » Functions » get_term_link

WordPress offers a lot of functions that make it easy to work with taxonomies, such as categories and tags. One such function is get_term_link, which is used to retrieve the link (URL) of a specific term within a taxonomy.

In simpler terms, suppose you have a website that has a custom post type called "Books", and you want to display all the books that belong to a particular category, say "Fiction". You can use the get_term_link function to retrieve the URL of the "Fiction" category page, which you can then use in your code to link to that page.

The get_term_link function takes two arguments: the first is the ID or object of the term whose link you want to retrieve, and the second is the name of the taxonomy to which the term belongs. It then returns the URL of the term’s archive page, which can be used to link to that page.

Here’s an example usage code for get_term_link:

$term_link = get_term_link( 23, 'category' );
echo '<a href="' . $term_link . '">Fiction</a>';

In this example, we’re using get_term_link to retrieve the link of the category with the ID of 23, and the name of the taxonomy is "category". We then echo out an anchor tag with the retrieved link, which displays "Fiction" as the link text.

Overall, get_term_link is a useful function for retrieving the link of a specific term within a taxonomy, which can be used to link to that term’s archive page.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.