Conditions

With conditions you can take control about to display the content of placeholder or not, depending on conditions you define.

If

With “if” you can check for example if a placeholder contains something you expect.

{% if "[post_type]" == "campaign" %}
    It's a campaign
{% endif %}

This example will output “It’s a campaign” if the Post Type of the post is “campaign”. Otherwise nothing will be outputted.

Else

If you want to output a default string, if a condition was not fulfilled, you can use the “else” condition.

{% if "[post_type]" == "campaign" %}
    It's a campaign
{% else %}
    It's something else
{% endif %}

This example will output “It’s a campaign” if the Post Type of the post is “campaign”. Otherwise it will output “It’s something else”.

Elseif

If you want to check for multiple conditions, you can use the “elseif” condition.

{% if "[post_type]" == "campaign" %}
    It's a campaign
{% elseif "[post_type]" == "job_listing" %}
    It's a job listing
{% elseif "[post_type]" == "event" %}
    It's an event
{% else %}
    It's something else
{% endif %}

and

You can combine multiple conditions with the and operator.

{% if "[post_type]" == "campaign" and "[post_categories]" == "advertisement" %}
    It's an advertisement campaign
{% else %}
    It's something else
{% endif %}

or

You can combine multiple conditions with the or operator.

{% if "[post_type]" == "campaign" or "[post_type]" == "subcampaign" %}
    It's a campaign or subcampaign
{% else %}
    It's something else
{% endif %}

Note

Check out the Twig documentation page for more details about the if statement.

Examples

not empty

Only show the Post excerpt if it is not empty.

{% if "[post_excerpt]" is not empty %}This is the excerpt: [post_excerpt]{% endif %}

empty

Check if [post_editlink] is empty in case of the user’s permission to edit a post has exceeded in the meantime.

{% if "[post_editlink]" is empty %}Your permission exceeded{% else %}[post_editlink]{% endif %}

in

Check if the Post tags contain “Books”. Makes use of the Containment operator.

{% if "Books" in "[post_tags]" %} This post has tag "Books" {% endif %}

Custom Fields

The combination of conditional statements and WordPress’ Custom Fields is real power!

For example, if you set up a Custom Field called comment-from-reviewer you can output it in a notification email only if a reviewer entered some text.

{% if "[post_custom_field-comment-from-reviewer]" is not empty %}
    Comment from reviewer: [post_custom_field-comment-from-reviewer]
{% endif %}