0

my categories have a custom field called titleTag for SEO as well as a number of other fields. When I render them at their URI (/categories/cat1) I can output those fields easily with {{category.titleTag}} etc. However on the main category page I also have a filter button that loads another URI via jQuery.load() to filter the items based on category. So in the filter template I have this to start.

{% set filter = craft.request.getParam('filter') %}
{% set category = craft.categories.slug(filter) %}

I then want to output (amongst other things) the custom field values of the found category like so {{category.titleTag}} but it comes out null (the category found I am testing with has the field filled out.

Where am I going wrong?

1 Answer 1

0

Because your category variable is no object of type craft\elements\Category but an ElementQuery in order to fetch the category you need to use the one() function

{% set category = craft.categories.slug(filter).one() %}  
{% if category != null %}
    {{ category.titleTag }}
{% endif %}

Your {{category.titleTag}} printed the search criteria. Imagine you would like to set other conditions in your query: how is Craft supposed to know when your query ends?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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