Conditional shortcodes for field connections (Themer)
Besides field connection shortcodes, which return the contents of field connections as text strings, Beaver Themer has conditional field connection shortcodes, which display custom text if the field connection shortcode meets the conditional if statement.
There are several types of conditional tests:
- Boolean if and if-else syntax
Tests whether the shortcode returns content or is empty.
- Test for text or integer value for WordPress custom fields
The shortcode text string is equal to or not equal to a text value, or a shortcode number value is greater than (>), greater than or equals (>=), less than (<), less than or equals (<=), equal to (=), or not equal to (!=) an integer value. See the Related article for how to test for values in WordPress custom field shortcodes.
- Test for text, number, email, or URL value in shortcodes for Advanced Custom Fields (third-party plugin) (introduced in Beaver Themer 1.1)
Enables testing shortcode values for Text, Number, Email, and Url-type fields. See the Related Articles for how to test for values in ACF field shortcodes.
Conditional shortcode syntax
Boolean If syntax
The conditional shortcode syntax introduced in the first versions of Beaver Themer is an if-statement that is a Boolean test. In other words, it tests for the presence or absence of the shortcode in the statement.
When would you want to use a Boolean If? For example, you have some text as a label for the field connection shortcode that follows, but you want the prefixed text to appear only when that field connection shortcode returns a value. Or, you're creating a custom post layout in a Posts module to display a featured image, but you only want the layout markup to be implemented when the post has a featured image. This Boolean if statement is also useful for troubleshooting field connection problems. See the Build This and troubleshooting articles in the Related Articles below for concrete examples with code.
Here's the syntax for the Boolean If conditional shortcode. Notice
wpbb-if has a beginning and end tag.
The conditional checks whether
some-field-connection-shortcode returns content and if it does, the custom text is inserted. Custom text can include HTML markup and shortcodes. The exception is that you can't nest conditional
[wpbb-if] shortcodes within each other.
To add the logical operator NOT before the field connection in the conditional, use the exclamation mark
!, as shown in this code. This syntax is paraphrased as "If some-field connection doesn't return any content, display the text 'Field connection content doesn't exist."
Boolean if-else syntax
You can add an optional
[wpbb-else] clause to an if-statement. Here's the syntax. Notice
wpbb-else doesn't have an end tag.
The conditional checks whether
some-field-connection returns content, and if it does, the custom text is inserted. If it doesn't, "my-other-custom-text" is inserted.
To continue the previous featured image example, you could set up an if-else statement that would be paraphrased as "Display this text if the featured image exists; otherwise, say 'No featured image'."