List of Primary Tags

This is a workaround.

  1. Loop through posts
  2. Loop through tags for each post. Limit the tags to one, which will be the primary tag.
{{#get 'posts' include='tags'}}
  {{#foreach posts}}
    {{#foreach tags limit='1'}}
    <li class='item' data-slug='{{slug}}'>
      <a href='{{ url }}'>{{ name }}</a>
    </li>
    {{/foreach}}
  {{/foreach}}
{{/get}}
  1. All the primary tags will be now visible but with duplication. With some JS, you can clean the duplicated ones.
<script>
$(document).ready(function() {
  var found = {};
  $('.item').each(function() {
    var $this = $(this);
    if(found[$this.attr('data-slug')]) {
      $this.remove();
    } else {
      found[$this.attr('data-slug')] = true;
    }
  });
});
</script>

Make sure jQuery is loaded before that JS code. JS code credit: https://mijokristo.com/remove-duplicate-elements-from-list-by-value-with-jquery/

Update (None jQuery Code):

<script>
  var finalList = { },
      elements = document.querySelectorAll('.item');

  elements.forEach( element => {
    if (finalList[element.dataset.slug]) {
      element.style.display= 'none';
    } else {
      finalList[element.dataset.slug] = true;
    }
  });
</script>
1 Like