Support » Fixing WordPress » get_the_tags and links

  • Resolved boon_

    (@boon_)


    I’m trying to call a list of tags for each post in a get_post query outside the loop.

    I can get it to return all the tags but not the links. I am using this code..

    <ul class="tagslist">
    <?php
       global $post;
       foreach(get_the_tags($post->ID) as $tag) {
          echo "<li><a href=\"<?php echo get_tag_link($tag_id); ?>\">$tag->name</a></li>";
       }
    ?>
    </ul>

    But all I get is <a href="<?php echo get_tag_link(0); ?>">Tag</a> 🙁

    Does anyone know how I can solve this?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Might need to add:

    global $wp_query;
    $wp_query->in_the_loop = true;

    Edit – fixed it with this 🙂

    echo '<li><a href="http://www.mysite.co.uk/tag/'.$tag->slug.'" rel="tag">'.$tag->name.'</a></li>';

    to make..

    <?php
       global $post;
       foreach(get_the_tags($post->ID) as $tag) {
    	  echo '<li><a href="http://www.mysite.co.uk/tag/'.$tag->slug.'" rel="tag">'.$tag->name.'</a></li>';
       }
    ?>

    Didn’t try the other

    @boon_ what if you have multiple tags. You solution works great, but I’m not sure how I can limit it to a single tag.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_the_tags and links’ is closed to new replies.