<p>It wasn't really clear to me if you want to list only those categories that are related to the current entry
or if you want a list of all categories that are assigned to your sermons.</p>

<p>I dont't think the group filter helps a lot in either case and I'd get the catgories like this.</p>

<pre><code>{% set sermonCategories = entry.series %}
</code></pre>

<p>In case you don't want them to be ordered by your manual sort order, you need to
get them using a <code>craft.categories</code> ElementCriteriaModel.</p>

<pre><code>{% set sermonCategories = craft.categories.relatedTo(entry).order('title') %}
</code></pre>

<p>To get all the categories you'd query for all of your "sermons" entries first.</p>

<pre><code>{% set sermons = craft.entries.section('sermons') %}
{% set sermonCategories = craft.categories.relatedTo(sermons).order('title') %}
</code></pre>

<p>The you can start with your loop. I did some small changes, so that only categories with entries related to it will be shown.</p>

<pre><code>{% for category in sermonCategories %}

    {% set relatedEntries = craft.entries.relatedTo(category) %}

    {# Only show category if there are related entries #}
    {% if relatedEntries|length %}

        {% set image = category.photos.first() %}
        {% set params = { width: 300, height: 200 } %}

        {# If a photo exists, show it, otherwise show a placeholder #}
        {% if image %}
            &lt;img  src="{{ image.getUrl(params) }}" height="{{ image.getHeight(params) }}" width="{{ image.getWidth(params) }}" alt=""&gt;
        {% else %}
            &lt;img  src="/img/cover-default.jpg" width="300" height="200" alt=""&gt;
        {% endif %}

        &lt;ul class="sermonadditional"&gt;
            {% for entry in relatedEntries %}
                &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
            {% endfor %}
        &lt;/ul&gt;

    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-04-01T20:09:46.583Z
Excellent. Thank you! I am beginning to understand how to use Craft and categories. So it looks like you set a variable and define its properties then call that variable to output the info.
glider
CC BY-SA 3.0
2015-04-01T05:37:18.603Z
2015-04-01T05:43:58.303Z
2015-04-01T05:43:58.303Z
carlcs
<p>I am guessing that <code>series</code> is your category field. In that case, you are grouping your entries by the title of their first (and only?) category. In the loop</p>

<pre><code>{% for category, catEntries in entries | group('series.first().title') %}
</code></pre>

<p><code>category</code> will be the title of the category, and <code>catEntries</code> will be the entries whose category has that title.</p>

<p>The title of a category is a string, so it makes sense you can't get <code>category.photos</code>.</p>

<p>What you want is a category, not a string. What I would do is this:</p>

<pre><code>{% for categorySlug, catEntries in entries | group('series.first().slug') %}
  {% set category = craft.category.slug(categorySlug).first %}
  ... rest of your code as before ...
{% endfor %}
</code></pre>

<p>I am grouping by category slug instead of title, because that is guaranteed to be unique, and then fetching the actual category based on its slug.</p>

CC BY-SA 3.0
2015-04-01T04:59:45.777Z
2015-04-01T04:59:45.777Z
Marion Newlevant
<p>I am trying to show an image that is defined on a category - named photos. I have an entry page with a list of all category entries listed at the bottom. I'd like to show this image but I'm still green on Craft categories and I'm getting errors in dev mode. This is what I have so far:</p>

<pre><code>{% set entries = craft.entries.section('sermons').find() %}
{% for category, catEntries in entries | group('series.first().title') %}

 {% if category.photos | length %} {# if a photo exists, show it #}
    {% for image in category.photos.limit(1) %}
    {% set params = { width: 300, height: 200 } %}
        &lt;img  src="{{ image.getUrl(params) }}" height="{{ image.getHeight(params) }}" width="{{ image.getWidth(params) }}" alt=""&gt;
    {% endfor %}
  {% else %} {# if no photo exists, show a default image #}
        &lt;img  src="/img/cover-default.jpg" width="300" height="200" alt=""&gt;&lt;/a&gt;
  {% endif %}


{# output all category entries #}   
    &lt;ul class="sermonadditional"&gt;
   {% for entry in catEntries %}
       &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
   {% endfor %}
   &lt;/ul&gt;
{% endfor %} 
</code></pre>

CC BY-SA 3.0
2015-04-01T03:04:37.940Z
What errors are you getting?
Brad Bell
CC BY-SA 3.0
2015-04-01T04:02:33.227Z
It looks like it fails right at the {% if category.photo ...  line. I get: 
"Impossible to access an attribute ("photos") on a string variable ("1 Corinthians")"
glider
<categories>
Category field in entry
# Category field in entry

(Asked by: username_0 on 2015-04-01)

I am trying to show an image that is defined on a category - named photos. I have an entry page with a list of all category entries listed at the bottom. I'd like to show this image but I'm still green on Craft categories and I'm getting errors in dev mode. This is what I have so far:
{% set entries = craft.entries.section('sermons').find() %}
{% for category, catEntries in entries | group('series.first().title') %}

 {% if category.photos | length %} {# if a photo exists, show it #}
    {% for image in category.photos.limit(1) %}
    {% set params = { width: 300, height: 200 } %}
        
    {% endfor %}
  {% else %} {# if no photo exists, show a default image #}
        
  {% endif %}


{# output all category entries #}   
    
   {% for entry in catEntries %}
       {{ entry.title }}
   {% endfor %}
   
{% endfor %}

**Tags:** categories

> **Comments:**
> **username_1**: What errors are you getting?  
> **username_0**: It looks like it fails right at the {% if category.photo ...  line. I get:  "Impossible to access an attribute ("photos") on a string variable ("1 Corinthians")"  

---

## 2 answers

### Answer by username_2 on 2015-04-01. Score: 3 | Accepted answer

It wasn't really clear to me if you want to list only those categories that are related to the current entry
or if you want a list of all categories that are assigned to your sermons.
I dont't think the group filter helps a lot in either case and I'd get the catgories like this.
{% set sermonCategories = entry.series %}

In case you don't want them to be ordered by your manual sort order, you need to
get them using a craft.categories ElementCriteriaModel.
{% set sermonCategories = craft.categories.relatedTo(entry).order('title') %}

To get all the categories you'd query for all of your "sermons" entries first.
{% set sermons = craft.entries.section('sermons') %}
{% set sermonCategories = craft.categories.relatedTo(sermons).order('title') %}

The you can start with your loop. I did some small changes, so that only categories with entries related to it will be shown.
{% for category in sermonCategories %}

    {% set relatedEntries = craft.entries.relatedTo(category) %}

    {# Only show category if there are related entries #}
    {% if relatedEntries|length %}

        {% set image = category.photos.first() %}
        {% set params = { width: 300, height: 200 } %}

        {# If a photo exists, show it, otherwise show a placeholder #}
        {% if image %}
            
        {% else %}
            
        {% endif %}

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

    {% endif %}

{% endfor %}

> **Comments:**
> **username_0**: Excellent. Thank you! I am beginning to understand how to use Craft and categories. So it looks like you set a variable and define its properties then call that variable to output the info.  

---

### Answer by username_3 on 2015-04-01. Score: 2

I am guessing that series is your category field. In that case, you are grouping your entries by the title of their first (and only?) category. In the loop
{% for category, catEntries in entries | group('series.first().title') %}

category will be the title of the category, and catEntries will be the entries whose category has that title.
The title of a category is a string, so it makes sense you can't get category.photos.
What you want is a category, not a string. What I would do is this:
{% for categorySlug, catEntries in entries | group('series.first().slug') %}
  {% set category = craft.category.slug(categorySlug).first %}
  ... rest of your code as before ...
{% endfor %}

I am grouping by category slug instead of title, because that is guaranteed to be unique, and then fetching the actual category based on its slug.


---

