Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I have a category set up and have added entries to it. How do I loop through these entries, and output just the title of each entry?

Serious noob here but trying to learn, any help would be gratefully received.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

This example assumes you have your Category Group setup to use custom URLs. In the template used to display your categories, the category variable will be available and represent your CategoryModel for that page.

The key is to pass the CategoryModel (in our example below category) to the relatedTo parameter of the Entries tag.

{{ category.title }}

{% paginate craft.entries
    .section('articles')
    .relatedTo(category)
    .limit(10) 
    as entries 
%}

    {% if entries|length %}

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

        {# Pagination #}

    {% else %}

        <p>No items have been added to this Category yet.</p>

    {% endif %}

{% endpaginate %}
share|improve this answer
    
Thanks Ben, this has allowed me to loop through all the entries in my channel (only have 1 as I'm using the personal version), I think I am missing something, maybe in the setup of my category group. I'll go back to it and have a root around, many thanks. –  bobbyballbags Jul 5 at 9:57

Here's another good example for working with categories that may be useful to you. This will loop through all categories (that have entries) and show their entries:

{% set entries = craft.entries.section('someSection').limit(null) %}

{% for category in craft.categories.group('someCategoryGroup').relatedTo(entries) %}
    <h2>{{ category }}</h2>

    {% for entry in entries.relatedTo(category) %}
        {{ entry.title }}
    {% endfor %}

{% endfor %}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.