Using WordPress ‘get_tag_link()’ PHP function

The get_tag_link() WordPress PHP function retrieves the link to a specific tag.

Usage

echo get_tag_link( $tag_id );

Replace $tag_id with the ID of the tag you want the link for.

Parameters

  • $tag (int|object): Required. Tag ID or object.

More information

See WordPress Developer Resources: get_tag_link()

Examples

In this example, we retrieve the tag link for the tag with an ID of 10 and display it:

$tag_id = 10;
echo get_tag_link($tag_id);

This example shows how to display the tag link for each tag of a post within the loop:

$tags = get_the_tags();
if ($tags) {
  foreach ($tags as $tag) {
    echo get_tag_link($tag->term_id);
  }
}

In this example, we display a list of tag links with their corresponding tag names:

$tags = get_tags();
foreach ($tags as $tag) {
  echo '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a>';
}

Create a tag cloud

This example creates a tag cloud by displaying tag links with their names and scaling the font size based on the tag count:

$tags = get_tags(array('orderby' => 'count', 'order' => 'DESC'));
foreach ($tags as $tag) {
  $font_size = 10 + ($tag->count / 2);
  echo '<a href="' . get_tag_link($tag->term_id) . '" style="font-size:' . $font_size . 'px;">' . $tag->name . '</a> ';
}

Display tags as a comma-separated list

This example displays tags as a comma-separated list of links:

$tags = get_tags();
$tag_links = array();
foreach ($tags as $tag) {
  $tag_links[] = '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a>';
}
echo implode(', ', $tag_links);