I have a template which displays all products (entries) for a particular channel:

Viewing all entries tagged with:<br>
{exp:channel:entries channel="products" category_group="1" category="not 7|8|9" status="retail|showroom|open" dynamic="no" orderby="title" sort="asc"}
        {exp:tag:tags entry_id="{entry_id}" backspace="2" tag_group_name="Attributes" orderby="tag_name" sort="desc"} {tag} |{/exp:tag:tags}

{exp:channel:entries channel="products" category_group="1" category="not 7|8|9" status="retail|showroom|open" dynamic="no" orderby="title" sort="asc" limit="8" paginate="bottom"}

<!-- all the sauce to display product entry -->

{switch='|||</div><div class="row row-category">'}{snp_paginate} {/exp:channel:entries}

<div class="row" style="text-align: center; margin-bottom: 10px;"> {exp:stash:get name='pagination_links' random limit="1" process="end"} </div>

but that is not working in a way I expect (some tags are displayed multiple times).

At that point my question is how can I get to display all tags (but not doubles) assigned to products (displayed using template above) from particular tag group?

Also is there any way to create something similar what NewEgg has on a left side bar, where customer can select "tags" and display only products which has those tags assigned?


To get tags for specific entries in one list, and without tags being repeated, {exp:tag:cloud} can't be used because there is no entry_id="" parameter. Further, the tag_group_id="" would list all tags in a tag group, not further filtered by specific entries.

Because {exp:tag:tags} is repeated at each {exp:channel:entries} loop, chances are you get duplicates of some tags. You'll need to create an array of unique tags to filter out duplicates. One way to do this is with PHP.

Enable PHP in your Templates and set it to Output. Then create your array:

{!-- Start with an empty array --}
<?php $tags = array(); ?>

{exp:channel:entries /* your parameters */}
   {exp:tag:tags entry_id="{entry_id}"}
      {!-- add the tag to the array. Duplicate tag_ids will be filtered this way --}
      <?php tags['{tag_id}'] = '{tag_name}'; ?>

Now you $tags array should have unique keys (the tag_id), and therefore tag_names should be unique as well. Finally, simply loop through the unique list of tags outside of and after {exp:channel:entries}. Below we're building a comma-separated list first, then trimming the final comma. Finally, we output the final string:

{!-- Start with an empty string --}
<?php $tag_list = ''; ?>    

<?php foreach($tags as $tag) :?>
   {!-- Concatenate tags --}
   <?php $tag_list .= $tag . ", "; ?>
<?php endforeach ?> 

{!-- Trim the final extra comma and display to screen --}
<?php echo rtrim($tag_list, ", "); ?>

There are likely other ways to do this, using Stash perhaps, but this uses native tools.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.