2

I have a channel which is basically a list of articles. Each article is associated with one or more companies. When I click on companies I want to show all the articles related to the company. I got that working, but now I want to show all the articles which have download types inside of them. So I had created a Matrix field type. I want to search for articles which will return all the articles which have those and then exclude them from other list which won't have any download association.

Is this doable?

1

Well done for working out how to display "reverse-related" entries!

In order to separate your Articles into two lists i.e.

  1. Articles with downloads
  2. Articles without downloads

... I would do something like the following on your Company single-entry template...

{% set relatedArticles = craft.entries.section('article').relatedTo() %}

{# Related articles which have downloads #}
{% for articleEntry in relatedArticles %}

    {# check for Matrix content #}
    {% if articleEntry.downloads != '' %}
        <h3>{{ articleEntry.title }}</h3>

        <ul>
            {# loop through any matrix items #}
            {% for block in articleEntry.downloads %}

                {# get the first asset into a variable named 'download' #}
                {% set download = block.downloadFile.first() %}

                <li><a href="{{ siteUrl }}{{ download.url }}">{{ block.downloadName }}</a></li>
            {% endfor %}
        </ul>

    {% endif %}
{% endfor %}

{# Articles which do not have downloads #}
{% for articleEntry in allArticles %}

    {# check for blank Matrix content #}
    {% if articleEntry.downloads == '' %}

        {# display article titles #}
        <h3>{{ articleEntry.title }}</h3>

    {% endif %}
{% endfor %}

Hope that helps. Let us know how you get on...

  • Thank you. I am going to write up this code and see if I get the desired results. I will definitely update you and mark this as an answer once I get this working. – Subrato M Jun 28 '16 at 14:18

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.