How to check if on a channel/category index? 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?
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.