1

Using the code below, Craft was able to get related tags to an entry, before I decided to move TagFieldType to MatrixFieldType.

{% set entries = craft.entries %}

{% for entry in entries %}

   <h3>{{ entry.title }}</h3>

   {% for tag in craft.tags.relatedTo(entry) %}

   <li>
     { tag.title }}
   </li>

 {% else %}

   <p>No tags found</p>

 {% endfor %}

 {% endfor %}

I'm quiet struggling to pull tags out so I rather share with you my Field's structure in my CP.

TagGroup

MatrixFields

TagField

I took many options to consider, Why I can't get it right, even found the post, that same handles may cause a problems, so I gave them all unique handles.

EntryFieldLayout

2

To get tags defined within a matrix field, you will first want to loop through the matrix-blocks in your matrix; and then loop through the tags in your tags field defined in each block.

{% for block in entry.myMatrixFieldHandle %}
    {% for tag in block.myTagsFieldHandle %}
        {{ tag.title }}
    {% else %}
        No Tags found.
    {% endfor %}
{% endfor %}

Also, for retrieving tags defined in a tags field on an entry (as in your example), you can simply retrieve the tags through the tags field directly, rather than using relatedTo(entry).

{% for tag in entry.myTagsFieldHandle %}
    {{ tag.title }}
{% else %}
    No Tags found.
{% endfor %}

For more information please see the documentation for tags and matrix fields.

Update

If you want to output your tags as you have them displayed in your screenshots, then use this:

    {% for block in entry.technologyField %}
        {% if block.type == "tag" %}
            {% for tag in block.technologyTag %}
                {{ tag.title }}
            {% else %}
                No Tags found.
            {% endfor %}
        {% endif %}
    {% endfor %}

However, I can also see some potential problems with your naming system and how you are using matrix blocks. Here are some suggestions for improvement.

  1. You don't need to end each Tag Group handle with 'TagGroup', as you already know that it's a Tag Group (so instead of 'technologyTagGroup', you can simply call it 'technology').

  2. You don't need to end each field handle with 'Field', as you already know that it's a field (so instead of 'technologyField', you can simply call it 'technology').

  3. Matrix block types support more than one field — I suspect that instead of having 3 separate block types defined, you actually only want 1 block type defined (let's call it 'technology' for example), with three fields in it: 'text', 'media', and 'tags' (or 'technologyTags' if you prefer).

  4. When using a Tags Field Type (or Assets, Entries, Categories or anything else with more than one item), it is generally a good practice to name the field handle plural so that you always know that it contains more than one of something (so instead of 'tag' the field should be called 'tags').

  5. If you followed #1, #2, #3, #4 above then the code to output your tags would be:

    {% for block in entry.technology %}
        {% for tag in block.tags %}
            {{ tag.title }}
        {% else %}
            No Tags found.
        {% endfor %}
    {% endfor %}
    
  6. If you later define another Block Type then when looping through the blocks you will need to check block.type, and only include those that have a Tags Field defined with the handle your looking for ('tags' in this case). For example:

    {% for block in entry.technology %}
        {% if block.type == "technology" %}
            {% for tag in block.tags %}
                {{ tag.title }}
            {% else %}
                No Tags found.
            {% endfor %}
        {% endif %}
    {% endfor %}
    
5
  • I've updated my question. I've tried your answer, but I haven't been able to get it. The entries are outputted on index page. Actually, I put in array a TagGroupsHandles, which are defined in Tag fields. Example: Matrix Field travel -> Tag Field tag (tag source: Tag Group travel). Is that correct with my update? Feb 23 '15 at 20:48
  • Your updated example won't work if your tag fields are defined in a matrix field. When a tags field (or any relationship field) is defined in a matrix field the relationship is technically created between the 'matrix field' and the 'tags', not the 'entry' and the 'tags'. The example I gave works, you just need to replace the 'myTagsFieldHandle' and 'myMatrixFieldHandle' with your actual field handles as defined in your field settings. If you have more than one related tags field, then you can just loop through both of them in order. Feb 23 '15 at 21:12
  • You must be right, but does it work under any fields structure? I couldn't get it working. It was kept saying thatCraft\EntryModel and its behaviors do not have a method or closure named tagFieldHandle. I've updated my questions with screenshots of my fields settings. Feb 24 '15 at 19:58
  • I updated the example. In the future, when someone uses a generic term like 'tagFieldHandle' you need to replace it with your actual tag field handle — the handle that you created to identify the field! Feb 24 '15 at 23:12
  • I like How you explain things with description. Actually my naming system was the same as you suggested in points #1, #2, #3, #4, It was more clearer for me, but I changed it back. I can't have block types structured as 'Technology, 'Travel', because at the editor's side with UserGroup permission to 'Travel' section only, will see all Block types, that don't relate to that UserGroup. Craft doesn't have that feature yet, If I read right. So with that in mind, I need to loop in all matrixFields, I don't know How yet. Feb 25 '15 at 20:23

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.