Displaying A Category’s Related Entries
You can fetch the entries related to a given category with a little help from the relatedTo
param.
From your category group’s template, it’d look like this:
{# Fetch all entries related to this category #}
{% set entries = craft.entries()
.relatedTo(category)
.all() %}
{# Output them #}
{% for entry in entries %}
<a href="{{ entry.url }}">{{ entry.title }}</a>
{% endfor %}
If you’re working outside your category template where the category
variable isn’t automatically available, first you’ll need to query your desired category:
{# Query a single category with the slug 'my-category' #}
{% set category = craft.categories()
.slug('my-category')
.one() %}
{# ... Continue with previous example ... #}
The relatedTo
param works the same with a categories field you’ve added to a custom field layout: loop over the categories or pick the one you want, then use it like that first example. From the entry’s template, it might look like this, where myCategoryFieldHandle
is the name of the field handle you chose for your custom field:
{# Get first related category from entry’s categories field #}
{% set category = entry.myCategoryFieldHandle.one() %}
{# ... Continue with first example ... #}