2

Been trying for days to figure this one out. How do I extract entries from a channel and display only entries related to a category? How do I pull in the related photo? I have this code below but it pulls in all the photos. I don't think I'm doing doing this right.

{% set category = craft.categories.slug('spring-summer').first() %}

{% for post in craft.entries.section('product').relatedTo({targetElement: category}) %}

    <h2>{{ post.title }}</h2>
    <p>{{ post.price }}</p>

    {% for product in craft.entries.find() %}
        {% for image in product.photo %}
            <img src="{{ image.getUrl('bottom') }}" />
        {% endfor %}
    {% endfor %}
{% endfor %}
2

When you set up your category, you check the Categories in this group have their own URLs box, and you give the category a URL (suppose it is category/{slug}), and a template (suppose it is category/_entry). Now in the category/_entry.html template, category will be defined (the way entry is defined on the template for a section entry).

How do I list the entries related to a category? (tucked away in the Get Help section of the Craft website) describes how to fetch and list the entries related to a category:

{# Fetch all of the entries related to this category #}
{% set entries = craft.entries.relatedTo(category) %}

{# Output them #}
{% for entry in entries %}
    <a href="{{ entry.url }}">{{ entry.title }}</a>
{% endfor %}

If you want to narrow the entries down to a single section, then fetch them like this:

{% set entries = craft.entries.section('mySectionHandle').relatedTo(category) %}

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.