cm_w89
cm_w89

Reputation: 167

shopify I need to check if current page is a collection page and not a single product page

I am trying to check if my current page is a collection page not a single product page in some collection.

I mean for example if someone goes to collection page of shoes then I can check using collection.handle == 'Shoes' but if I select a product from that page then it will still give me true But I want my condition to be true on if it is collection page. Thanks for your Help!

Upvotes: 13

Views: 52137

Answers (6)

Alice Girard
Alice Girard

Reputation: 2173

Use this simple way with template:

{% if template contains 'collection' %}
    Do something
{% endif %}

As Shopify evolves you can now use this:

{% if template.name == 'collection' %}
    Do something
{% endif %}

As Shopify evolves (bis) you can now use this:

{% if page.request_type == 'collection' %}
    Do something
{% endif %}

Upvotes: 20

nh-labs
nh-labs

Reputation: 679

since templates subsets have a ".", like collection.xyz, page.xyz, product.xyz I would use this code

{%- assign t = template | split: '.' | first -%}
{%- if t == 'page' -%}
{%- endif -%}
{%- if t == 'product' -%}
{%- endif -%}
{%- if t == 'collection' -%}
{%- endif -%}

Upvotes: 0

MatteoOreficeIT
MatteoOreficeIT

Reputation: 199

For defining a site wide page type I like this :

<body _pagetype="{{ request.page_type | handle }}">

Upvotes: 0

Kishan Mehta
Kishan Mehta

Reputation: 21

You can try this in condition.

{%if request.page_type == "collection" %} 
--- True if visit collection page or sub collection page. ---
{% endif %}

Thanks.

Upvotes: 2

codinghands
codinghands

Reputation: 1801

A safer alternative to avoid rare cases where templates are badly named would be to use request.page_type - see the Shopify docs

{% if request.page_type == 'collection' %}
  Do something
{% endif %}

Upvotes: 13

Allsops
Allsops

Reputation: 133

I would use:

{% if template == 'collection' %}
  Do something
{% endif %}

Because if you use contains and not == (equal to) you may run risk of it changing down the line if you ever created a new template which contained the word collection?

Upvotes: 6

Related Questions