WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_the_tags Codex example not working (7 posts)

  1. kasperbs
    Member
    Posted 5 years ago #

    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 . ' ';
    }
    }
    ?>
  2. MichaelH
    Member
    Posted 5 years ago #

    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.

  3. kasperbs
    Member
    Posted 5 years ago #

    Thanks, that works better :)

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

  4. MichaelH
    Member
    Posted 5 years ago #

    $mytag = $tag->name;

  5. kasperbs
    Member
    Posted 5 years ago #

    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.

  6. AsraiLight
    Member
    Posted 4 years ago #

    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?

  7. AsraiLight
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags