WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Walker
    Member
    Posted 4 years ago #

    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);
    if(!empty($posttags)){
    foreach($posttags as $this_tag){
    $tagdisplay = $tagdisplay . "".$this_tag->name.", ";
    }
    }
    echo substr($tagdisplay,0,-2); // remove the comma after last tag
  2. Walker
    Member
    Posted 4 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:

    $tagdisplay='';
    unset($tagdisplay); // failsafe
  3. sellerscentral
    Member
    Posted 3 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 3 years ago #

  5. deepbevel
    Member
    Posted 3 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. jettrush
    Member
    Posted 3 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
    Member
    Posted 3 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
    Member
    Posted 3 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. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

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

  10. deepbevel
    Member
    Posted 3 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