Skip to main content

Condition Patterns

This page shows ready-to-use condition patterns you can adapt for your projects.

Existence checks

{% if post.thumbnail %}
<img src="{{ post.thumbnail.src('large') }}" alt="{{ post.thumbnail.alt ?? post.title }}">
{% endif %}

{% if not post.thumbnail %}
<img src="/default-image.jpg" alt="">
{% endif %}

Excerpt with fallback to content

{% if post.excerpt %}
<p class="excerpt">{{ post.excerpt }}</p>
{% endif %}

{% if not post.excerpt %}
<p class="excerpt">{{ post.content|striptags|truncate(150) }}</p>
{% endif %}

Custom field

Only show a section when the field has a value:

{% if post.meta('subtitle') %}
<p class="subtitle">{{ post.meta('subtitle') }}</p>
{% endif %}

Authentication

Logged-in vs guest

user.id is 0 for guests, so it acts as a logged-in check:

{% if user.id %}
<p>Welcome back, {{ user.display_name }}!</p>
{% endif %}

{% if not user.id %}
<p><a href="/login">Log in</a> to access more features.</p>
{% endif %}

Role-based content

{% if 'editor' in user.roles %}
<a href="{{ post.edit_link }}">Edit this post</a>
{% endif %}
{% if 'editor' in user.roles or 'administrator' in user.roles %}
<div class="admin-toolbar">
<a href="{{ post.edit_link }}">Edit</a>
</div>
{% endif %}

Taxonomy

Badge based on category

{% if post.category.slug == 'featured' %}
<span class="badge badge--featured">Featured</span>
{% endif %}

Tag check

{% if 'sale' in post.tags %}
<span class="badge badge--sale">On Sale</span>
{% endif %}

Different layout by category

Use two Condition blocks with opposite expressions:

{% if post.category.slug == 'video' %}
<div class="card card--video">
{{ post.meta('video_embed') }}
<h3>{{ post.title }}</h3>
</div>
{% endif %}

{% if post.category.slug != 'video' %}
<div class="card">
<img src="{{ post.thumbnail.src('medium') }}" alt="{{ post.thumbnail.alt ?? post.title }}">
<h3>{{ post.title }}</h3>
</div>
{% endif %}

Custom fields

Conditional styling from meta

{% if post.meta('highlight_color') %}
<div style="border-left: 4px solid {{ post.meta('highlight_color') }}">
{{ post.content }}
</div>
{% endif %}

ACF true/false field

{% if post.meta('is_featured') %}
<span class="featured-star"></span>
{% endif %}

Combined conditions

Multiple checks with AND

{% if user.id and 'editor' in user.roles %}
<a href="{{ post.edit_link }}">Edit this post</a>
{% endif %}

Multiple checks with OR

{% if post.category.slug == 'news' or post.category.slug == 'updates' %}
<span class="badge">Latest</span>
{% endif %}

Grouped conditions

Use parentheses to control evaluation order:

{% if post.post_status == 'publish' and (post.thumbnail and post.excerpt) %}
<div class="highlight">{{ post.title }}</div>
{% endif %}

Loop + Condition

Empty loop fallback

When a loop returns no items, its content doesn't render. Place a Condition block after the loop to show a fallback:

{% for post in get_posts() %}
<article>
<h2>{{ post.title }}</h2>
</article>
{% endfor %}

{% if not posts %}
<p>No posts found.</p>
{% endif %}

Conditional content inside a loop

Inside a Loop block, conditions can check each item's data:

{% for post in get_posts() %}
<article>
<h2><a href="{{ post.link }}">{{ post.title }}</a></h2>

{% if post.thumbnail %}
<img src="{{ post.thumbnail.src('medium') }}" alt="{{ post.thumbnail.alt ?? post.title }}">
{% endif %}

{% if post.category.slug == 'featured' %}
<span class="badge">Featured</span>
{% endif %}
</article>
{% endfor %}
Common mistake

When negating a condition with not, make sure you're checking the same expression. {% if post.thumbnail %} and {% if not post.thumbnail %} work as expected, but {% if post.thumbnail %} and {% if post.thumbnail == false %} may not — an empty value is not the same as false.

Next steps