Forums

[resolved] get_the_tags and links (4 posts)

  1. boon_
    Member
    Posted 3 years ago #

    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

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    Might need to add:

    global $wp_query;
    $wp_query->in_the_loop = true;
  3. boon_
    Member
    Posted 3 years ago #

    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

  4. mrwangkai
    Member
    Posted 3 years ago #

    @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.

Topic Closed

This topic has been closed to new replies.

About this Topic