1

This is the code used to display entries related to categories.

The issue: if an entry belongs to 2 categories, it displays all the categories to which it belongs on the page.

I need Craft to display only the category and entries the (lastSeg) of the slug (selected from a nav).

Can't find where to start?

{% set entries = craft.entries.section('contenuCompositions').relatedTo(category).order('dateYear','comp_opus','title').limit(0) %}
{% for category in craft.categories.group('compositions').relatedTo(entries).level(2).slug(lastSeg) %}
<div class="container">
<div class="row">
    <div class="col-md-12">
        <h1>{{ category }}</h1>
    </div>
</div>
{% for row in entries.relatedTo(category)|batch(3) %}
<div class="row">
    {% for entry in row %}
      <div class="col-md-4">
        <p>{{ entry.title }}
      </div>
    {% endfor %}
</div>
</div>
{% endfor %}
</div>
2

Not sure if this is still what you are after (I removed a lot of parameters), but basically this gets the one (!) category with the unique (!) slug you request from the URL and then lists the entries related to it:

{% set categorySlug = craft.request.getLastSegment() %}
{% set category = craft.categories.group('compositions').slug(categorySlug).first() %}

<h1>{{ category.title }}</h1>

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

{% for entry in entries %}
    {{ entry.title }}
{% endfor %}
  • This works perfectly. Added a category template. Thanks @carlcs – jjem Nov 20 '14 at 8:13

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.