Skip to main content

If Page.tag_slug == "

Hi,

We have a need for the basic page template to display differently based on a condition and what tag the author has added to the page.

I was under the impression you can make a conditional statement in liquid using a page tag (name, ID or slug) but I cannot get it to work correctly, Below is my code and other code i have tried.

{% if page_tag.id == "5933" %}{% include pdf_template %} {% else %} rest of template....


{% if page_tag.slug == "pdf" %}{% include pdf_template %} {% else %} rest of template....


{% if page_tag.name == "pdf" %}{% include pdf_template %} {% else %} rest of template....


{% if page.page_tag.name == "pdf" %}{% include pdf_template %} {% else %} rest of template....


{% if page.page_tag == "pdf" %}{% include pdf_template %} {% else %} rest of template....


I have tried other bits of code as well, so i'm figuring either you can't actually have a condition like this or I am completely off of being close to the correct usage. Thanks in advance!

Official response from

Hey Emm,

To check page tags and display content accordingly, the logic is slightly different to a typical if conditional. You'll want to use the following snippet, modified to fit your tag naming structure:

{% assign tag1 = false %}
{% assign tag2 = false %}

{% for tag in page.tags %}
{% if tag.name == 'tag1' %} //tag1 is first tag name
{% assign tag1 = true %}
{% endif %}
{% if tag.name == 'tag2' %} //tag2 is second tag name
{% assign tag2 = true %}
{% endif %}
{% endfor %}

{% if tag1 %}
show content associated with first tag name 
{% endif %}

{% if tag2 %}
show content associated with second tag name 
{% endif %}

 

Hope that helps!

Share this post

Showing 4 reactions

How would you tag this suggestion?
Please check your e-mail for a link to activate your account.