• Resolved brandonchance

    (@brandonchance)


    I’m trying to add Tags to a Category List and I’ve been successful, but my issue, they’re not just returning their own tags but the tags for every post.

    Example – How it Should Be:

    Post 1: [Tag 1]
    Post 2: [Tag 2]
    Post 3: [Tag 3]

    Example – How It Is:

    Post 1: [Tag 1] [Tag 2] [Tag 3]
    Post 2: [Tag 1] [Tag 2] [Tag 3]
    Post 3: [Tag 1] [Tag 2] [Tag 3]

    I pulled the following code from the codex:

    <?php
    $tags = get_tags();
    $posttags = '<div class="post_tags">';
    
    foreach ( $tags as $tag ) {
      $tag_link = get_tag_link( $tag->term_id );
    
      $posttags .= "<a href='{$tag_link}' title='{$tag->name}' class='{$tag->slug}'>";
      $posttags .= "{$tag->name}</a>";
    }
    
    $posttags .= '</div>';
    echo $posttags;
    ?>

    Any help or direction is greatly appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter brandonchance

    (@brandonchance)

    I just switched out get_tags for the_tags and that fixed the issue, but now it says “Tags:” before it, and it’s not wrapped in a p/span tag that I could hide.

    A solution for this would be fine as well as it would give the same result

    Thread Starter brandonchance

    (@brandonchance)

    Nevermind, I switched out the_tags for get_the_tags and the issue resolved! Thanks for looking!

    Final code for future reference:

    <?php
    $tags = get_the_tags();
    $posttags = '<div class="post_tags">';
    
    foreach ( $tags as $tag ) {
      $tag_link = get_tag_link( $tag->term_id );
    
      $posttags .= "<a href='{$tag_link}' title='{$tag->name}' class='{$tag->slug}'>";
      $posttags .= "{$tag->name}</a>";
    }
    
    $posttags .= '</div>';
    echo $posttags;
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_tags Includes All Post Tags In Array’ is closed to new replies.