Get Term URL by ID
January 7, 2025 ยท AI Conversations
User
get term url using term id
Assistant
To get the URL of a WordPress term using its term ID, you can use the `get_term_link()` function. Here's how:
### Example:
$term_id = 123; // Replace 123 with your term ID
$term_link = get_term_link((int) $term_id);
if (!is_wp_error($term_link)) {
echo $term_link; // Output the term URL
} else {
echo "Error: Unable to retrieve term URL.";
}
### Explanation:
– **`get_term_link()`**: Accepts a term ID, term object, or term slug and returns the term URL.
– **`is_wp_error()`**: Checks if the result is a WordPress error object to handle errors gracefully.
This works for any taxonomy term, including categories, tags, or custom taxonomies.