2

I’m using a structure for creating new pages and navigation. In this structure there are multiple entry types where one is used for creating category pages. Here a user can add some content and select a category that will be used to load projects that are related to it.

In my template I'm using this code to do this:

{% set category = entry.projectCategory.first().slug %}
{% set params = {
        section: 'projects',
        relatedTo: category
} %}
{% set projectEntries = craft.entries(params) %}

    <h1>{{ category }}</h1>

    <div>
{% for projectEntry in projectEntries %}
        <div>
            <a href="{{ projectEntry.url }}">
                <div class="index-image">
                    {% set image = projectEntry.featuredImage.first() %}{% if image %}<img src="{{ image.getUrl('fullImage') }}" alt="{{ image.title }}">{% endif %}
                </div>
                <div class="index-title">
                    <h2>{{ projectEntry.title }}</h2>
                </div>
            </a>
        </div>
{% endfor %}
</div>

First I'm getting the category slug that was selected for this category page. After that I'm setting up my parameters for getting projects (this is another section) that are related to that category.

Before it worked perfectly fine when I had put in a "static" category (so not using the category variable) but when I tried to push the selected category the whole thing stopped working. Even going back to my original code that worked doesn't load any projects.

If I remove the "relatedTo" argument it loads all projects just fine.

Also the slug is perfectly shown in the H1 tag.

Any ideas?

3

You are so close! All you need to do is to remove the ’.slug’, because that is what the param is expecting.

  • Nice, perfect! This is my first Craft/Twig project so these little things pop up a lot haha. – Maarten van Gent Sep 2 '17 at 8:12

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.