0

Should be easy i thought. How can i display all entries from a section from a certain category?

This is my code now, but nothing shows up (screenshot bottomleft):

{% set category = craft.categories.slug('b2c').first() %}
{% set entries = craft.entries.section('homeBlocks').relatedTo(category).find() %}

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

This code shows all entries (screenshot topleft), and you can see there are entries with b2c:

{% set category = craft.categories.slug('b2c').first() %}
{% set entries = craft.entries.section('homeBlocks').find() %}

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

screen shot

My Category Group is called Business Category (with handle "businessCategory"). It contains two categories:

  • b2c (slug "b2c")
  • b2b (slug "b2b")

The field handle used in the section "homeBlocks" is "businessSegment"

Where's the error? :)

  • What happens if you take the find() off of your relatedTo? So it becomes: {% set entries = craft.entries.section('homeBlocks').relatedTo(category) %} – Damon Dec 3 '15 at 18:08
  • @damon same result: no entries – Dave Driesmans Dec 3 '15 at 18:32
  • I think I understand what you are trying to get. I've updated my answer - see if that gets you closer to what you need. – Damon Dec 4 '15 at 12:34
1

Give this a try. I'm not 100% sure I've got my site set up the same, but I think it's close.

I have a category group called Categories. It looks like this:

Categories
    b2a    
    b2b
    b2c

I created a section called homeBlocks which is a channel (shouldn't matter). Then on my entry, I have a Categories field with a handle of categoryOne.

On my template:

{% set category = craft.categories.slug('b2c') %}
{% set entries = craft.entries.section('homeBlocks').relatedTo(category).find() %}

{% if entries | length %}
    {% for entry in entries %}
        <p>{{ entry.title }}</p>
    {% endfor %}
    {% else %}
        {# No entries found. #}
{% endif %}

I have created 3 entries within my homeBlocks channel, and I've assigned the 1st entry and the 3rd entry to b2c.

Now I am seeing this when I view the page:

<p>Entry Three</p>
<p>Entry One</p>

EDIT

Sorry for the bad info - I think I understand what you are looking for now. I've updated my answer so give it another try.

  • Thanks Damon. Tried it but It shows all entries like my second code example... but i only want the entries with category slug "b2c". So i guess in your example without "Entry Two b2c Business Segment" – Dave Driesmans Dec 3 '15 at 19:22

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.