Dynamic Shortcodes for Expressing Conditions
Updated over a week ago

Using the if and comparison Dyanmic Shortcodes

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

In its basic form, the if DS interprets a condition passed as first argument. If the conditions is true, it returns the second argument; if false, it returns the third argument.
โ€‹
To read it in plain english you can imagine it like this:
โ€‹{if: CONDITION (then) RETURN-THIS (else) RETURN-THIS} Here I added (then) and (else) just for explaining the idea, but should not be included in the DS.

Some example with real DSes:

  • {if:1 2 3} here the first argument (the condition) is 1, since 1 is considered true the result of the DS is the second argument: 2.

  • {if:0 2 3} here the first argument 0 is considered false, the result of the DS is the third argument (the else part): 3.


    Of course in real usage the condition would not be static like 0 and 1 but something that has a different result depending on the context.

The if DS is often paired with comparison DSes, 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 DSes allows for conditional displays.

For example,

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

shows A if A < B, else shows B.

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.

Switch Dynamic Shortcode

The switch DS tests a value against a list of values, returning a result based on matches.

Called with a value (X) and pairs of test values and results, switch compares X against each test value. If X matches, the corresponding result is returned. If no match is found, it yields null or a default value, if provided.

For example,

{switch: 5 1 'apple' 3 'banana' 'fruit'} 

with X as 5 returns 'fruit', as 5 matches neither 1 nor 3. With X as 3, it returns 'banana'.

Examples and Practical Usage

  1. {if:{and:{gt:A 10} {lt:A 20}} 'Within range' 'Out of range'}

    Displays 'Within range' if A is between 10 and 20.

  2. {switch: X 1 'One' 2 'Two' 'Other'}

    Displays 'One' if X is 1, 'Two' if X is 2, and 'Other' if X is neither.

  3. {if:{not:{eqv:A B}} 'Not equal' 'Equal'}

    Displays 'Not equal' if A is not equal to B, else displays 'Equal'.

Integration with Other Dynamic Shortcodes

The if and comparison DSes can be integrated with other DSes to create more complex conditional logic.

Practical Examples

  1. Using {acf:field} with {if}:

    • {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'.

  2. Comparing the Value of an ACF Field:

    • {if:{eq:{acf:color} 'Red'} 'Red Color' 'Other color'}

      • Here, if the value of the ACF field color equals 'red', 'Red Color' is displayed. Otherwise, 'Other color' is shown.

  3. Combining Multiple Conditions:

    • {if:{and:{eq:{acf:gender} 'female'} {lt:{acf:age} 25}} 'Youth Female Category' 'Other Categories'}

      • In this example, if the gender field is 'female' and the age field is less than 25, 'Youth Female Category' is displayed. Otherwise, 'Other Categories' is shown.

  4. Utilizing {post:title} with {if}:

    • {if:{eq:{post:title} 'Special Post'} 'This is a Special Post' 'Regular Post'}

      • This displays 'This is a Special Post' if the title of the post is 'Special Post'. If the title is anything else, it displays 'Regular Post'.

These examples demonstrate how DSes can be combined to create dynamic, conditional logic based on the specific content of a page or post. By using if, eq, gt, lt, and other comparison and logical DSes, dynamic and interactive displays can be crafted based on the content.

Did this answer your question?