Using WordPress ‘get_term_link()’ PHP function

The get_term_link() WordPress PHP function generates a permalink for a taxonomy term archive.

Usage

get_term_link($term, $taxonomy = '');

Example

Input:

$term = 5;
$taxonomy = 'category';

Output:

https://example.com/category/term-name/

Parameters

  • $term (WP_Term|int|string) (Required): The term object, ID, or slug whose link will be retrieved.
  • $taxonomy (string) (Optional): Taxonomy. Default is an empty string.

More information

See WordPress Developer Resources: get_term_link()

Examples

// Get terms from the 'species' taxonomy
$terms = get_terms('species');
echo '<ul>';
foreach ($terms as $term) {
    // Get the term link
    $term_link = get_term_link($term);

    // If there is an error, continue to the next term
    if (is_wp_error($term_link)) {
        continue;
    }

    // Print the term link
    echo '<li><a href="' . esc_url($term_link) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
$dr_terms = get_terms('your_term_name');
if (!is_wp_error($dr_terms) && !empty($dr_terms)) {
    echo '<ul class="your-class-name">';
    foreach ($dr_terms as $dr_term) {
        echo '<li><a href="' . get_term_link($dr_term->slug, 'your_term_name') . '">' . __($term->name) . '</a></li>';
    }
    echo '</ul>';
}
$terms = get_terms('species');
if (!is_wp_error($terms) && !empty($terms)) {
    echo '<ul>';
    foreach ($terms as $term) {
        echo '<li><a href="' . get_term_link($term->slug, 'species') . '">' . $term->name . '</a></li>';
    }
    echo '</ul>';
}
$term_id = 5;
$taxonomy = 'category';
$term_link = get_term_link($term_id, $taxonomy);
echo 'Term link: ' . $term_link;
$term_slug = 'term-name';
$taxonomy = 'category';
$term_link = get_term_link($term_slug, $taxonomy);
echo 'Term link: ' . $term_link;