I'm attempting to create a tag-based filter on a category page, so I only want to show those tags that are used by posts in that category.
I've got it mostly working, but the list isn't coming out alphabetical, and tags are repeated if they're used by more than one post. Here's my code:
<ul class="dropdown filter option-set clearfix" data-filter-group="tags">
<li><a href="#filter-tags-all" data-filter-value=".item" class="selected">All Tags</a></li>
<?php $the_query = new WP_Query( 'posts_per_page=50&cat=89' ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$posttags = get_the_tags();
foreach($posttags as $posttag)
{
echo "<li><a href='#filter-tags-" . $posttag->slug . "' data-filter-value='.".$posttag->slug."'>" . $posttag->name . "</a></li>";
}
endwhile;
?>
</ul>
The issue seems to be that the foreach loop is within the while loop, so it's naturally echoing out the tags post by post. I can't figure out how to combine the results into one array.
I tried looping through $posttags
and using array_merge
as outlined here, but I couldn't get it to work.
Any ideas on how to combine the tags into one alphabetical list with no repeats would be much appreciated.