1

I am building a site where I have users, companies and content.

Currently my users are standard Craft users, and my companies and content are entries within a channel.

I have a field in my content called company name which is an asset field and lets me create a relationship between a piece of content and a company and now I want to create a comaopny page that lists all content "owned" by that company.

I have the below as a starting point which is listing all of my content but any help on what I'd need to do to tweak it so that it only displays content where company name is equal to the entry title (which is the company name)? Hopefully that makes some kind of sense!

Many thanks in advance!

{% set relatedEntries = craft.entries()
    .companyName(':notempty:')
    .all() %}

{% if relatedEntries|length %}
    <ul>
        {% for rel in relatedEntries %}
            <li><a href="{{ rel.url }}">{{ rel.title }}</a></li>
        {% endfor %}
    </ul>
{% endif %}
2
  • 1
    So, you have a 'companies' channel and a 'content' channel? How have you related the two channels?
    – 4midori
    Commented Nov 14, 2019 at 19:18
  • I have a field type (entries) that I use in my 'content' and choose the relevant company. As I say the above returns all content that has the company field filled out I just need to add logic that says only show those where companyName = entry.title
    – Chrisl2310
    Commented Nov 15, 2019 at 9:59

1 Answer 1

1

Well, you can do:

{# Get segment 2 from the URL #}
{% set company = craft.app.request.getSegment(2) %}

{# Get content where myCompanyField matches segment 2 #}
{% set entries = craft.entries.section('mySection').myCompanyField(company).all() %}

But I need to know more about the structure of your content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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