Skip to main content
Dynamic Shortcodes for Expressing Conditions
Updated over a week ago

Conditional Content with the 'if'

The if Dynamic Shortcode is a valuable tool for conditionally displaying or returning content based on a comparison's result.

In its basic form, the if shortcode interprets a condition passed as first argument. If the conditions is true, it returns the second argument; if false, it returns the third argument.

Basic Syntax

The if shortcode evaluates a condition, and based on this evaluation, it returns content:

{if:condition-here return-when-true return-when-false}
  • condition-here: The condition to be evaluated (must resolve to true or false).

  • return-when-true: The content or value returned if the condition is true.

  • return-when-false: The content or value returned if the condition is false (optional).

​If the third argument is omitted, nothing will be displayed if the condition is false:

{if:condition-here return-when-true}


  • {if:{acf:field} 'Field exists' 'Field does not exist'}

    This example displays 'Field exists' if the field specified by {acf:field} has a value (i.e., it is not empty or null); otherwise, it displays 'Field does not exist'.

  • {if:{post:featured-image-id} 
    'The Post has a Featured Image'
    'The Post does not have a Featured Image'

    • This shortcode checks if the current post has a featured image. The {post:featured-image-id} dynamically retrieves the ID of the featured image associated with a post.

    • If the post has a featured image, the ID will be a positive number (true), triggering the true condition.

    • If there is no featured image set, the result will be false (or equivalent to no value), and the false condition is displayed.

  • {if:{post:featured-image-id} 
    {media:image @ID={post:featured-image-id}}
    • {post:featured-image-id}: Retrieves the ID of the featured image. Acts as a condition check.

    • {media:image @ID={post:featured-image-id}}: This is nested inside the if shortcode. If the condition is true, this shortcode executes using the ID from {post:featured-image-id} to fetch and display the actual image in HTML format.

Comparison Shortcodes

The if is often paired with comparison Dynamic Shortcodes, which return true or false based on argument comparisons. For example:

  • gt (greater than): {gt:A B} equals A > B

  • lt (less than): {lt:A B} equals A < B

  • ge (greater than or equal): {ge:A B} equals A >= B

  • le (less than or equal): {le:A B} equals A <= B

  • eq (strict equality): {eq:A B} equals A === B

  • eqv (equality): {eqv:A B} equals A == B

Combining if with comparison Dynamic Shortcodes allows for conditional displays.

For example,

{if:{lt:A B} A B} 

shows A if A < B, else shows B.

Examples of Using if with Comparison Dynamic Shortcodes

  • Display Price Range

    {if:{gt:{product:price} 100} 'High-end Product' 'Affordable Product'}

    This checks if the price of a product is greater than 100. If it is, it labels the product as a "High-end Product"; otherwise, it is labeled as "Affordable Product".

  • Age Verification for Content Access

    {if:{ge:{user:age} 18} 'Access granted' 'Access denied'}

    This shortcode checks if the age of the user is 18 or older. If the condition is true, access to certain content is granted; if not, access is denied.

  • Adjust Content Based on User Experience Level

    {if:{lt:{user:experience_years} 5} 
    'Beginner Tips'
    'Advanced Strategies'

    Useful for educational content, this checks if the user's experience level is less than 5 years. New users see "Beginner Tips," while more experienced users see "Advanced Strategies."

  • Membership Renewal Reminder

    {if:{le:{user:days_until_membership_expires} 30} 
    'Your membership expires soon, renew now!'
    'Membership active'

    Checks if the user's membership expires in 30 days or less. If true, it prompts them to renew; otherwise, it confirms their membership is active.

  • Display Custom Message Based on Scores

    {if:{eq:{user:quiz_score} 100} 
    'Perfect score! Congratulations!'
    'Try again for a perfect score!'

    This checks if the user's quiz score is equal to 100. A perfect score displays a congratulatory message, while any other score encourages trying again.

Implementing Logic in Content with Logical Shortcodes

Additional shortcodes provide logical operations:

  • and: Logical AND. {and:{gt:A B} {lt:A C}} returns true if A > B and A < C.

  • or: Logical OR. {or:{eq:A B} {eq:A C}} returns true if either A == B or A == C.

  • not: Logical NOT. {not:{eq:A B}} returns true if A != B.

Logical Shortcodes Explained

  1. Logical AND (and)

    Syntax: {and:condition1 condition2}

    This shortcode returns true if both conditions are true. It's useful for cases where you need to meet multiple criteria simultaneously.

    Example: Checking Price and Stock for a Discount Offer

    {if:{and:{gt:{product:price} 100} {gt:{product:stock} 10}} 
    'Discount Available!'
    'No Discount'

    This checks if a product's price is greater than 100 and if its stock is greater than 10. Only if both conditions are satisfied does it offer a discount.

  2. Logical OR (or)

    Syntax: {or:condition1 condition2}

    Returns true if at least one of the conditions is true. It is useful when any of multiple criteria can qualify for a particular outcome.

    Example: Membership or Age for Access

    {if:{or:{eq:{user:member_status} 'premium'} {ge:{user:age} 21}}
    'Access Granted'
    'Access Denied'

    Access is granted if the user is a premium member or if they are 21 or older, meeting any of these conditions grants access.

  3. Logical NOT (not)

    Syntax: {not:condition}

    Returns true if the given condition is false. This is useful for reversing a condition's logical value.

    Example: Show a message if there is no billing address

    'Please update your billing address.'

    This shortcode checks if there is a billing address associated with the user's account. The {user:billing_address} would typically return the billing address if it exists. If no billing address is set, this might return an empty string, depending on how your shortcode is configured.

Simplifying Multiple Conditions with the 'switch' Shortcode

The switch Dynamic Shortcode allows you to compare a given value against a series of potential matches, executing corresponding outputs for each match, similar to a switch-case statement in programming. This shortcode simplifies decision-making in your templates by neatly handling multiple conditions without requiring nested or multiple if statements.


match_1 result_1
match_2 result_2 ...
  • test_value: The value to test against the match cases.

  • match_1, match_2, ...: Values to compare against test_value.

  • result_1, result_2, ...: Outputs corresponding to each match case.

  • default_result: Returned if test_value matches none of the provided cases.

Practical Example Using User Status

Imagine we want to display different messages based on the user's status, which can be 'active', 'inactive', or 'pending':

'active' 'Access granted'
'inactive' 'Please activate your account'
'pending' 'Account verification in progress'
'Access denied'
  • {user:status}: Retrieves the status of the current user, which is expected to be a simple, singular value like 'active', 'inactive', or 'pending'.

  • The shortcode evaluates the user's status:

    • If 'active', it displays "Access granted."

    • If 'inactive', it displays "Please activate your account."

    • If 'pending', it displays "Account verification in progress."

    • For any other status or if the status is undefined, it defaults to "Access denied."

Did this answer your question?