Support » Developing with WordPress » Can duplicate array elements be removed from the tag list?

  • Resolved eadwig



    I would like to remove duplicate array elements from the tag list. I’m having to accomplish this using the_tags() or get_tag_list() because the tag links could not be retrieved by the combination of get_the_tags() and get_tag_link($tag->ID) or get_tag_link($tag->term_ID).

    I know of the generic PHP function, array_unique(), that can be used to remove array duplicates. However, the function cannot be used to remove the duplicates retrieved by the_tags() or get_tag_list().

    Please someone advise me how to remove duplicates from the_tags() or get_tag_list() or else how to retrieve tag links properly so I can use get_the_tags() and array_unique() to customize the tag list manually to the same end.

    Thank you in advance.


    • This topic was modified 1 year, 2 months ago by eadwig.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz


    One approach that comes to mind is to loop through a list of all tags. For each name encountered, check if its name is in an array of tag names. You start with an empty array, so the first tag name certainly is not in the array. If not in the array, add the current tag name to the array and output the tag’s link. If a name already exists in the array, it’s a duplicate. Do nothing but continue looping through the remaining terms.

    Thank you for the tips. The advice given by @bcworkz makes perfect sense, so I tried the solution linked by @ketanambaliya.

    I inserted code as shown below but $tag->term_id only returns one tag ID, hence I’m failing to retrieve the IDs of the rest of the tags:

     $category = get_query_var('cat');
      $categories = get_category($category);
     $tagIDs = array();
     if(have_posts()) : while(have_posts()) : the_post();
     $tags = get_the_tags();
      foreach($tags as $tag){
       if(!in_array($tag->term_id, $tagIDs)):
        $tagIDs[] = $tag->term_id;
        $tagNames[$tag->term_id] = $tag->name;
     endwhile; endif;
     echo '<ul>';
     foreach($tagIDs as $tagID):
      echo '<li><a href="'.get_tag_link($tagID).'">'.$tagNames[$tagID].'</a></li>';
     echo '</ul>';

    I cannot work out what is preventing the foreach() block from retrieving all of the tag IDs for the life of me. Please someone help me work out what’s wrong with the code.

    Apparently, the problem was to do with the way the pagination was set up, so I mark this question as resolved.

    The code of the tag list as shown above can only show the tags of posts being displayed on the paginated page and it hides all other tags.

    Is there a way to show all of the tags on a paginated page?

    The code above began showing all of the tags once the posts_per_page greater than the number set in the admin screen was passed to WP_Query(). All’s fine now 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can duplicate array elements be removed from the tag list?’ is closed to new replies.