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!
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!
Showing 4 reactions
Sign in with
NationBuilder Directory Builder
Peter VanKoughnett Big River Web Design
Certified Architect
This code snippet is nice though. I’ve used it quite a bit.
NationBuilder Directory Builder
Peter VanKoughnett Big River Web Design
Certified Architect
NationBuilder employee