Test for values in WordPress custom field shortcode
You can use conditional shortcode to test a text or integer value returned by the shortcode for a WordPress custom field.
Tip
You can also use condiitional shortcode to test for the presence or absence of a WordPress custom field value.
For example, you can test a shortcode with a statement such as "If the value of this shortcode is "coffee" then display this text, or else display this other text," or "If the value of this custom field shortcode is less than 2, then display this text or else display this other text."
The syntax is the same as for the Boolean-If and the if-else conditional shortcode statements, with extra parameters:
The exp
parameter is the operator, with the following values:
- For text strings, you can use the following operators:
equals
notequals
- For integers, you can use the following operators:
equals
notequals
greater
greaterequals
less
lessequals
The value
parameter is the value that you want to test for. This can be a text string or an integer. Note that you can test for a date value as long as it is in the form YYYYMMDD, so the value can be compared as an integer.
Note
The value
parameter is case-sensitive.
#
Value expression examplesThe following examples are written so the actual value in the post being previewed is displayed for comparison. These examples are based on three WordPress custom fields with the following names:
wp_text_value
wp_number_value
wp_date
Custom WordPress fields don't have data types, but these three fields were named for testing purposes to indicate what the value would be: a text string, an integer, or a date in the format YYYYMMDD.
This code was inserted into an HTML module in a Singular Themer layout to test values for WordPress custom fields that had been added to those posts. Look at the output in each example to help understand the code.
equals
example#
1. Text Output:
notequals
test example#
2. Text Output:
equals
test example#
3. Number Output:
greater
test example#
4. Number Output:
greaterequals
test#
5. Number Output:
equals
test#
6. Date Output:
less
test#
7. Date Output:
lessequals
test#
8. Date Output: