1

I'm currently working on integration some caching behaviour using {% cache %} tags. This works mostly just fine, but I have some issues when I work with related entries.

For example I have a list of categories with an indicator to show many entries they are assigned to.

  • New (3)
  • Used (2)
  • Broken (3)

If I change an entry and add a category, the cache refreshes and shows the correct amount. But if I remove a category from an entry I get the wrong number until I refresh the cache manually.

Is this the expected behaviour? Or do I miss something here?

Here is my code example:

{% cache unless not craft.config.cache %}
{% set categorySlug = productSection ~ 'Categories' %}

<ul>

{% for category in craft.categories.group(categorySlug) %}
  {% set entryCount = craft.entries.relatedTo(category).total() %}
  {% if entryCount > 0 %}

    <li>
      {{ category.title }} ( {{ entryCount }} )
    </li>

  {% endif %}
{% endfor %}

</ul>

{% endcache %}
1

This sounds like a bug, and I think I know why. Can you try changing the {% set entryCount ... line to this?

{% set entryCount = craft.entries.relatedTo(category.id).total() %}
|improve this answer|||||
  • Sure, thanks. I get the same behaviour. Adding works, removing not. – Pascal Mollet Nov 3 '14 at 7:58

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.