The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Get Current Post Tags (Without Link to Tag Page) (10 posts)

  1. Walker
    Posted 6 years ago #


    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
  2. Walker
    Posted 6 years ago #

    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
  3. sellerscentral
    Posted 5 years ago #

    I'm trying to do this for http://www.coloringpagesite.com my coloring page site but I dont understand...what is the "loop"?

  4. esmi
    Forum Moderator
    Posted 5 years ago #

  5. deepbevel
    Posted 5 years ago #

    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.

  6. Nick
    Posted 5 years ago #

    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).

  7. deepbevel
    Posted 5 years ago #

    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?

  8. deepbevel
    Posted 5 years ago #

    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); ?>
  9. Michael
    Forum Moderator
    Posted 5 years ago #

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

  10. deepbevel
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic