This WordPress Codex document clearly shows how to get the name of the first tag of a post:

$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    if (1 == $count) {
      echo $tag->name . ' ';

How do I modify the aforementioned code, so that a hyperlink of the tag is shown, instead of just the name?

1 Answer 1


Instead of

echo $tag->name . ' ';


echo get_tag_link( $tag->term_id );

See the Codex on get_tag_link() and term_id.

And encapsulate the code in a function. Put this into your functions.php:

function wpse_49056_first_post_tag_link()
    if ( $posttags = get_the_tags() ) 
        $tag = current( $posttags );
            '<a href="%1$s" itemprop="url"><span itemprop="title">%2$s</span></a>',
            get_tag_link( $tag->term_id ),
            esc_html( $tag->name )

In your single.php you just call the function where you need the link:

<?php wpse_49056_first_post_tag_link(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.