Support » Themes and Templates » Get Current Post Tags (Without Link to Tag Page)

  • Hello.

    I’ve been struggling to get it working for an hour now.

    Inside the loop I want to display the post tags associated with a post, but without a link, just a plan, simple, comma-separated list of tags.

    This automatically excludes the_tags() which only creates a list with links to tag pages.

    Then I’ve tried this:

    $posttags = get_the_tags($post->ID);
    foreach($posttags as $this_tag){
    $tagdisplay = $tagdisplay . "".$this_tag->name.", ";
    echo substr($tagdisplay,0,-2); // remove the comma after last tag

Viewing 9 replies - 1 through 9 (of 9 total)
  • I’ve quickly figured it out.

    I had to clean the contents of $tagdisplay before the loop rotates for the second post, because the contents of the variable were prepended to the tags of current post.

    A better example:
    You have posts named 1, 2, 3, 4 and 5.
    Each post has 1 tag: tag1, tag2, tag3, tag4, tag5.
    The loop runs for post 1 and displays tag1, the value of tag1 is inserted into $tagdisplay.
    The loop runs for post 2, but the above code caused it to display the value of $tagdisplay and then display any other tags, associated with the current post, causing an output: tag1, tag2.

    So the fix is easy as inserting this above the code in the first post:

    unset($tagdisplay); // failsafe

    I’m trying to do this for my coloring page site but I dont understand…what is the “loop”?



    Forum Moderator

    Since no one who would actually know is answering..I think it has something to do with placing the code inside the php brackets, rather than outside. Although I’m not sure if it only applies to functions.php, I think so.

    Going Up,

    Thanks for your solution! This worked perfectly for me.

    sellerscentral, deepbevel,

    esmi’s link has more info for a complete understanding but the loop is the area of your code that looks for posts and then performs the same action on the ones that it finds (usually spitting out the title and content of the post into the code).

    So, for instance, after

    <?php if ( have_posts() ) : ?> 				
    <?php while 
    (have_posts()) : the_post(); ?>

    and before

    <?php endwhile; ?>
    <?php endif; ?>

    is that correct?
    What about <?php else : ?>
    does it have to close with an end?

    What is the difference when you put code inside

    <?php(existing code here); (new code here); ?>

    rather than

    <?php(existing codes here); ?> <?php (new code here); ?>


    Forum Moderator

    no difference in the result;
    maybe minute difference in the procesing speed – i would assume the first one is faster.

    Going Up,
    What is the basic code to get tags from posts, and only show the tags without the posts, like a tag cloud? I want to preserve the link from the tags to the posts(s), rather than make a list.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Get Current Post Tags (Without Link to Tag Page)’ is closed to new replies.