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.
Thread Starter
eadwig
(@eadwig)
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:
if(is_category()):
$category = get_query_var('cat');
$categories = get_category($category);
endif;
$tagIDs = array();
query_posts('category_name='.$categories->slug);
if(have_posts()) : while(have_posts()) : the_post();
$tags = get_the_tags();
if($tags):
foreach($tags as $tag){
if(!in_array($tag->term_id, $tagIDs)):
$tagIDs[] = $tag->term_id;
$tagNames[$tag->term_id] = $tag->name;
endif;
}
endif;
endwhile; endif;
wp_reset_postdata();
echo '<ul>';
foreach($tagIDs as $tagID):
echo '<li><a href="'.get_tag_link($tagID).'">'.$tagNames[$tagID].'</a></li>';
endforeach;
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.
Thread Starter
eadwig
(@eadwig)
Apparently, the problem was to do with the way the pagination was set up, so I mark this question as resolved.
Thread Starter
eadwig
(@eadwig)
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?
Thread Starter
eadwig
(@eadwig)
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 🙂