WordPress.org

Ready to get started?Download WordPress

Forums

get_the_tags() problems (3 posts)

  1. timodav
    Member
    Posted 5 years ago #

    Hi Guys,

    I am using this code to get some tags for a post

    <?php
    $posttags = get_the_tags();
    if ($posttags) {
      foreach ($posttags as $tag) {
         $tagnames[count($tagnames)] = $tag->name;
      }
      $comma_separated_tagnames = implode(", ", $tagnames);
      print_r($comma_separated_tagnames);
    }
    ?>

    The problem i am facing when i use this code is that tags are "aggregated" i.e. it prints tags at the top post fine - only the top post tags appear, but then in the 2nd post - it prints the top post tags and the second post tags and so on. third post, 1st, 2nd and 3rd post tags etc etc how to restrict tags in this code, to just show for one post ?

    i.e. this code works perfect from the get_tags() page

    <?php
    $posttags = get_the_tags();
    if ($posttags) {
    foreach($posttags as $tag) {
    echo $tag->name . ' ';
    }
    }
    ?>

    and it returns only the tags for each post. The code I am using works great since I can print echo $comma_separated_tagnames but I'm not a PHP coder so not sure how to change the code to not make to "count up / aggregate" the tags - and instead just show tags per individual post ?

    Please help?

  2. MichaelH
    Member
    Posted 5 years ago #

    After this line:
    $comma_separated_tagnames = implode(", ", $tagnames);

    need to clear your array
    unset($tagnames);

  3. MichaelH
    Member
    Posted 5 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic