Function and usage of get_tag_link() in wordpress


Answers ( 1 )


    The get_tag_link() function in WordPress is used to retrieve the URL link to a specific tag page. This function is part of the WordPress template tags, which are used to display or return data related to WordPress themes and posts.

    Function Signature

    get_tag_link( int|object $tag ): string


    • $tag (int|object, required): The ID of the tag or the tag object itself. You can pass either the numeric ID of a tag or a tag object to this function.

    Return Value

    • string: The function returns the URL link to the tag page. If the specified tag does not exist, it returns an empty string.


    The get_tag_link() function is a convenient way to get the URL of a tag’s archive page. This is useful when you want to create links to a tag archive within your theme or plugins. The function ensures that the correct link is returned, respecting the permalink structure set in the WordPress settings.

    Usage Example

    Here’s a simple example of how to use get_tag_link() in a WordPress theme or plugin:

    $tag_id = 123; // Replace with your tag ID
    $link = get_tag_link($tag_id);
    if (!empty($link)) {
        echo '<a href="' . esc_url($link) . '">View posts tagged with this tag</a>';
    } else {
        echo 'Tag not found.';

    In this example, we're retrieving the link for a tag with an ID of 123. If the tag exists and the link is successfully retrieved, it creates a hyperlink to the tag's archive page. If the tag does not exist, it outputs a 'Tag not found.' message.

    See Also

    • get_term_link(): Similar to get_tag_link(), but for any term in a taxonomy, not just tags.

    Best Practices

    • Always validate and sanitize the output of get_tag_link() before using it in your HTML code, especially if the tag ID comes from user input. Use functions like esc_url() for safety.
    • Check if the tag exists and if the function returns a valid link before using it, to avoid broken links on your website.

Leave an answer