3

In the previous question I asked for a way to detect the 404 page from a plugin. Brandon gave me a good starting point with this answer. However, I wondered what method I could use to also detect wether the user is on any category/channel index page?

1
  • I'm not entirely sure I understand what you're asking. Can you try to add an example or more specific details? Commented Aug 13, 2014 at 18:01

1 Answer 1

3

That would require some sort of convention on your end which identifies what makes up a category/channel index page, as channels and categories don’t actually have an “index URL”.

For example you could decide that all sections’ index pages should have a URI that matches the section’s handle (not necessarily a good idea though, in the event that you want a URI that includes dashes). In that case you could do something like:

{% set segments = craft.request.getSegments() %}
{% if segments|length == 1 %}
    {% set section = craft.sections.getSectionByHandle(segments[0]) %}
    {% if section %}
        We’re on a section index!
    {% endif %}
{% endif %}

There is no corresponding function for getting a category group by its handle at this time, however.

A better approach might be to just leave it up to the templates to define whether they are supposed to be an index template. So in news/index.html, the template that you want to treat as the News index template, for example, you could put this:

{% set isIndex = true %}

Any layout templates that the index template extends will then have access to that isIndex variable.

2
  • As always, thank you Brandon. Just what I was after. Commented Sep 1, 2014 at 11:47
  • It would be good to highlight this in the docs. Maybe as a 'common gotchas' tip.
    – ponies
    Commented May 5, 2020 at 17:17

Your Answer

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

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