.. _template_conditions: ##################### Conditions ##################### .. highlight:: jinja 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. .. code:: {% 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. .. code:: {% 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. .. code:: {% 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. .. code:: {% 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. .. code:: {% 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. .. code:: {% 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. .. code:: {% 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 `_. .. code:: {% 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. .. code:: {% if "[post_custom_field-comment-from-reviewer]" is not empty %} Comment from reviewer: [post_custom_field-comment-from-reviewer] {% endif %}