Forums

[resolved] Display Tags, But Not As Links (4 posts)

  1. Jiglet
    Member
    Posted 3 years ago #

    Is it possible to display tags in WordPress, but not have them link anywhere? I'd like the tags to just show up as plain text. Please let me know if I can do this - Thanks!

  2. Jiglet
    Member
    Posted 3 years ago #

    I thought I found the solution to this, but I was wrong. I feel that this may be an option somewhere in the admin panel, but I've looked everywhere! If anyone can help me out here I'd really appreciate it - Thanks!

  3. tici
    Member
    Posted 3 years ago #

    I will provide you in a few minutes with a very simple function. Just wait, don't go.

  4. tici
    Member
    Posted 3 years ago #

    <?php
    /**
    * Strips html from tags
    *
    * @param array $array_of_tags
    * @param bool (true or false) true = prints out the tags / false = saves them into one variable for later usage
    * @return string
    */
    if(!function_exists('strip_html_from_tags'))
    {
          function strip_html_from_tags($tags, $echo = true)
          {
                $originalArray = $tags; // keep original array intact for now
                $lastTag = array_pop($tags); // we only need the name from this
    
                $separator = ", "; // Replace comma with another character as your new separator
                $output    = "";
    
                foreach($originalArray as $tag) // loop throught the $originalArray to capture each tag name and save them in the $output variable
                {
                      $output .= $tag->name;
                      // determine the use of the separator
                      $output .= ($tag->name == $lastTag->name) ? '' : $separator; // if we're on the $lastTag, delete de $separator
                }
                unset($lastTag); // no need this anymore
    
                // Display type
                if($echo)
                {
                      echo $output; // prints them on screen
                }
                else
                {
                      return $output; // saves them for later parsing (maybe to link them back?)
                }
          }
    }
    
    /*--------------------------------------------
    | Usage example - you have to be in the loop |
    ---------------------------------------------*/
    
    // Ex 1: print your tags
    $tags = get_the_tags();
    strip_html_from_tags($tags); // this will echo out the tags
    
    echo "";
    
    // Ex 2 (using the 2nd parameter)
    $tags = get_the_tags();
    $tags = strip_html_from_tags($tags, false); // you may save them for later usage
    
    // An example of the above usage (passing the 2nd parameter) would be:
    $myStrippedTags = explode(', ', $tags);
    // now $myStrippedTag is an array of tags - do something to each of them
    foreach($myStrippedTags as $tag)
    {
         // Maybe you would like to link them back?
         // ...but I found this unnecesary to link them back - because you will reinvent the "the_tags" function
         echo "<a href='".get_bloginfo('url')."/tag/{$tag}' title='{$tag}'>{$tag}</a>";
    }
    ?>

    Well - I did the best I can to help you, as my english isn't the best.
    Hope you will find this useful.

    Bye now.
    Tici

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags