Support » Themes and Templates » get_the_tags is random? Why?

  • Resolved 36pix

    (@36pix)


    I’m running the following code having followed several examples in the forums and I am having the same wild-card results as others. Some people claim they get random results, others get just the tags for the current post (what I want) and others get no results at all.

    Well, my results are also different. I get a random number of tags along with random tags. ie, every time I reload the page I get any number of tags (never know how many I’ll get) and the tags are from any number of posts.

    What I want is to only display the tags for the current post. This code doesn’t do that. I’m running the code from my functions.php

    get_the_tags();
    $posttags = get_the_tags();
    if ($posttags) {
        foreach($posttags as $tag) {
            echo '<li><div>' . $tag->name . '</div></li>';
        }
     }
Viewing 1 replies (of 1 total)
  • Man oh man. Got it working. I had to add the following to my function:

    global $wp_query;
    $post = $wp_query->post->ID;

    I thought this was available in functions.php naturally. The rest looks like this:

    $tags = wp_get_post_tags($post);
    
    if ($tags) {
        foreach($tags as $tag) {
            $tag_link = get_tag_link($tag->term_id);
            echo "<li><div><a href='$tag_link' class='used-tag' rel='tag'>".($tag->name)."</a><em>" . $post . "</em></div></li>";
        }
    }

Viewing 1 replies (of 1 total)
  • The topic ‘get_the_tags is random? Why?’ is closed to new replies.