<p>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:</p>

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

&lt;h1&gt;{{ category.title }}&lt;/h1&gt;

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

{% for entry in entries %}
    {{ entry.title }}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-11-20T08:13:49.023Z
This works perfectly. Added a category template. Thanks @carlcs
jjem
CC BY-SA 3.0
2014-11-19T17:09:58.047Z
2014-11-19T17:09:58.047Z
carlcs
<p>This is the code used to display entries related to categories.</p>

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

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

<p>Can't find where to start?</p>

<pre><code>{% 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) %}
&lt;div class="container"&gt;
&lt;div class="row"&gt;
    &lt;div class="col-md-12"&gt;
        &lt;h1&gt;{{ category }}&lt;/h1&gt;
    &lt;/div&gt;
&lt;/div&gt;
{% for row in entries.relatedTo(category)|batch(3) %}
&lt;div class="row"&gt;
    {% for entry in row %}
      &lt;div class="col-md-4"&gt;
        &lt;p&gt;{{ entry.title }}
      &lt;/div&gt;
    {% endfor %}
&lt;/div&gt;
&lt;/div&gt;
{% endfor %}
&lt;/div&gt;
</code></pre>

<categories>
List entries related to several categories
# List entries related to several categories

(Asked by: username_0 on 2014-11-19)

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) %}


    
        {{ category }}
    

{% for row in entries.relatedTo(category)|batch(3) %}

    {% for entry in row %}
      
        {{ entry.title }}
      
    {% endfor %}


{% endfor %}

**Tags:** categories


---

## 1 answers

### Answer by username_1 on 2014-11-19. Score: 2 | Accepted answer

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() %}

{{ category.title }}

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

{% for entry in entries %}
    {{ entry.title }}
{% endfor %}

> **Comments:**
> **username_0**: This works perfectly. Added a category template. Thanks @username_1  

---

