Support » Themes and Templates » get_the_tags Codex example not working

  • Resolved kasperbs

    (@kasperbs)


    Hi I was looking at this codex page and found this solution for showing the first tag associated with a post:

    <?php
    $tag = get_the_tags();
    if ($tag) {
    $tag = $tag[0]; echo $tag->name;
    }
    ?>

    Only problem is that it doesn’t work for me, it shows absolutely nothing.

    Funny thing is that, this works without a problem and shows all the tags associated with the particular post.

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • Corrected that example with:

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

    Thanks for the heads-up on that.

    Thanks, that works better 🙂

    Is there a way to store that output in a variable?

    $mytag = $tag->name;

    That was a little embarrassing, so simple. I thought I tried that but obviously did something wrong.

    Thanks for you quick response on this request.

    I must be doing something wrong, because I’ve tried copying the code exactly both from that page in the codex and from this post, and it’s returning no tags whatsoever…please help?

    I found the answer (that works for me) here.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘get_the_tags Codex example not working’ is closed to new replies.