Show tag icons instead of tag text. (4 posts)

  1. mamunabms
    Posted 5 years ago #

    I would like show tag icons instead of tag icons.
    As an example, Instead of the tag text "Photoshop" I would like to show Photoshop icon.
    Any help would be cordially appreciated.

  2. Michael
    Forum Moderator
    Posted 5 years ago #

    you could use some code, starting with 'get_the_tags()':


    for instance:

    $separator ='';
    $posttags = get_the_tags();
    if ($posttags) {
      $img_path = get_bloginfo('stylesheet_directory');
      foreach($posttags as $tag) {
        $image = 'tag_icon_' . $tag->slug . '.jpg';
        $link = get_tag_link( $tag->term_id);
        $output .= $separator . '<a href="' . $link . '">';
        $separator =' '; // this adds a space between the icons; you can change it
          if(file_exists(STYLESHEETPATH.'/images/'.$image)) {
          $output .= '<img src="' . $img_path.'/images/'.$image . '" alt="tag ' . $tag->name .' icon" />';
          } else {
          $output .= $tag->name;
        $output .= '</a>';
      echo $output;


    the icon image needs to be in the /images folder of the theme;
    named tag_icon_tag-slug.jpg

    for instance, your tag name is 'Beautiful Day Today' then the image name has to be tag_icon_beautiful-day-today.jpg

    the code checks for the image, and outputs the tag name if the image is not found.

  3. shuggar
    Posted 5 years ago #

    thanks for the reply, this code is working for me.
    Yet there is something wrong with it, when I insert it into the loop.

    I am displaying the icons on the post excerpts within categories and archives, but this not work correct.

    The first post will be displayed correct, the second one for example contains its tags, but it also contains the tags of the first post and so on.

    All the tags get added to following posts. It does not display the icons only for the appropriate post.

    Somehow the php query is not getting closed. Unfortunately I am no php expert, therefore I am not able to point my finger on it.

    Thanks for clearing this up.

  4. Michael
    Forum Moderator
    Posted 5 years ago #

    it is caused by the way the output string is added together from bits.

    at the beginning of the code, after this line:
    $separator ='';
    set the $output to empty;

    $output = ''; //initialize clean output;

Topic Closed

This topic has been closed to new replies.

About this Topic