1

Trying to get all entries related to a particular blog category to output to the page. Right now I just get the correct URL for any category and a blank page.

Also my categories are entered into a matrix field as one of the matrix blocks. I use a single matrix field to capture all of the content for each blog entry.

Here is my categories/_category.html template code.

{# Fetch all of the entries related to this category #}
{% set category = craft.categories.slug('') %}
{% set entries = craft.entries.relatedTo(category) %}

{# Output them #}
{% for entry in entries %}
    <a href="{{ entry.url }}">{{ entry.title }}</a>
{% endfor %}

I've tried plugging in particular category slugs into the craft.categories.slug('') but no help. I'm following advice from https://craftcms.com/support/entries-related-to-category

....and

Category Entries

Any ideas on what is missing?

1

First you'll need to know what you are currently doing

craft.categories.slug()

Will return a CategoryQuery with no property attached to it. So when you execute this query you'll fetch all categories. Your so called category variable is no category but a query that needs to be executed in order to do something

craft.entries.relatedTo(category)

Same thing again: it will return an EntryQuery with all entries that related to any category in any field. So as long as an entry has a relation to what every category it will match. Then again entries is not an array of entries but a query that needs to be executed via all()

When you loop through this query it is executed as well but with a deprecated warning because this way is not supported anymore.

If you are in a category page you'll likely have the catetory variable already injected as superglobal in your template. So there is already a category you have access to.

You can then do

{% for entry in craft.entries.relatedTo(category).all() %}

Please make sure there are entries that are related to your current category

1

So I finally solved this, and wanted to post the answer here for anyone that stumbles on this similar issue. All of the content for each of my blog entries is input through a single matrix field. I also included a block in that field to hold all of the entry categories, and most of my entries have multiple categories. I could not get those entries to output to the front end, no matter what I tried, until I took the categories out and made them a separate field.

So I now have a matrix field for my blog entry main body content, images, and videos...and a separate field for categories. Once I did that, I was able output all blog entries related to any category, and switch among the different categories to get a new summary of entries based on the category picked. There may be some way to get the previous method to work, but I sure could not solve it. In any case - all is working as planned.

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.