This is one of the ways you can use conditional shortcodes to test various aspects field connection shortcodes. See the Overview article for general information.
You can use field connection shortcode to do a boolean test for the presence or absence of content returned by a regular field connection shortcode. It works for any type of field connection as long as it can be used as a shortcode – in other words, any field connection that returns a text string. It works with built-in WordPress field connections, WordPress custom fields, Advanced Custom Fields (ACF), and Pods custom fields.
Here's the basic logic when the conditional shortcode is used this way:
<some field connection shortcode>returns content, then
<display this text>else
<display some other text>
wpbb-if has a beginning and end tag.
To add the logical operator NOT, use the exclamation mark
!, as shown in this code. The following syntax is paraphrased as "If some-field-connection-shortcode doesn't return any content, display the text 'Field connection content doesn't exist."
The if-else statement has this syntax:
wpbb-else doesn't have an end tag.
Example 1: Label for custom field
You're using shortcode to display the value of an optional custom field, such as Hobbies, and you want to add a label before the value, such as:
Hobbies: chainsawing, weight lifting
If the custom field has no content, you don't want the label "Hobbies:" to appear, so you can wrap your text with conditional shortcode so it displays only when a custom field has content. In this example code, you can see that the ACF field connection shortcode appears both in the conditional shortcode and in the custom text area, which also includes HTML markup and the label:
Example 2: Markup for an optional field
If you include markup for a field, you probably only want that markup to appear when the field has something to display. The Posts module's layout code uses this conditional shortcode to display the featured image:
You could add an else clause to the featured image code to display the text "No featured image" when one doesn't exist:
In this example, you'd have to write a CSS rule for the
.no-featured-image class to position the text properly.
Example 3: Display an icon if Pods custom field has content
Suppose you have a Pods custom field to indicate that a location is accessible, and when it is accessible you want to display a Font Awesome icon of a wheelchair. Because you can only test whether the shortcode for a Pods custom field returns Text, you can't use a Yes-No field type to indicate accessibility. The Yes-No field doesn't return text.
One workaround is to use a Text field and enter directions to type Yes when the location is accessible and leave the field blank when it isn't, such as the example in this screenshot:
You can then test for the presence or absence of content in that field and insert an icon if there is content. Note that from the point of view of the conditional test, the field can contain any text, not just yes, to be true. However, this is the only option you have in Beaver Themer to convert a Pods custom field to an icon only on the condition that there is accessibility. Here's the code, which could be inserted into an HTML module.
This code says if the shortcode for the
wheelchair_accessible field has content, insert an
<i> tag for the Font Awesome Solid wheel hair icon at size 2x. See the Font Awesome documentation for information about how to insert and style icons in HTML.
For another Pods example, see this tutorial for a Pods example.
Example 4: Troubleshoot field connection problems
The Boolean if-else statement is useful for troubleshooting field connection problems. See these articles for concrete examples with code: