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