Toolset allows you to display content conditionally. This includes evaluating conditions based on your own custom functions.

Before using a function inside a conditional, you need to register it for security reasons. Even WordPress functions need to be registered. To do so, visit the ToolsetSettings page and click the Front-end Content tab. There, simply add your function to the Functions inside conditional evaluations section.

You can pass parameters to your functions or class methods, as follows:

  • Strings, wrapped in single quotes: my_func('my_value_1', 'my_value_2', ...)
  • Numbers, not wrapped: my_func(1, 2, ...)
  • Booleans, not wrapped: my_func(true, false, ...)
  • Null, not wrapped: my_func(null, ...)

Important: by default, all the functions used inside evaluation expressions will be passed these two parameters:

  • The current query type. Defaults to posts. When used inside a View, this will be the kind of object being listed: posts, taxonomy or users.
  • The current displayed object. Defaults to the current global post being displayed. When used inside a view, it will be the current object: for views listing posts, the current post object; for views listing taxonomies, the current term object; for views listing users, the current user object.

These parameters are always the last two ones of the function, so all your custom parameters should be set before them.

This is important because you should always set the optional parameters for your functions when you use them inside wpv-conditional shortcodes. If you do not set the optional parameters in your function, they will be passed as those described above, so it may have unexpected results.

When you compare what your functions return, you should follow this guidelines:

  • The function must return a string, a number or a boolean. If the function does not exist, or returns null, an array or an object, the evaluation will fail.
  • You can compare the returned value using the standard comparison functions: eq, ne, lt, gt, lte, gte
  • If you expect the function to return a string, the value to check against should be wrapped in single quotes: if="my_func() = 'my_value'"
  • If you expect the function to return a number, the value to check against can be wrapped in single quotes, but it’s not mandatory: if="my_func() = 2" and if="my_func() = '2'" will both work.
  • If you expect the function to return a boolean, the value to check agains should be 1 or ‘1’ for true and 0 or ‘0’ for false.
  • To check if your function is returning an empty value, compare it to an empty string: if="my_func() = ''"

Also, you should remember that:

  • If the function does not exist, the evaluation will fail.
  • As said above, if you call a function without filling the optional parameters or your function has mandatory parameters that are not passed, you can get unexpected results including PHP notices and warnings.

Checking For Empty Post Content

In order to check if a post has empty content, you could use the shortcode[wpv-post-body view_template="None"] and compare it against an empty string or use the empty() function. However, using that shortcode triggers some WordPress filters, so even if the post has indeed an empty content it could return a non empty string.

Instead, you can use a custom function to check this. First, insert this code in your theme’s functions.php file:

Checking For Empty Post Content
function wpv_conditional_post_has_content($type, $object) {
    $return = 0;
    if ( $type == 'posts' ) {
        if ( empty( $object->post_content ) ) {
            // return 0 when post's content is empty
            $return = 0;
        } else {
            // return 1 when post's content is not empty
            $return = 1;
        }
    }
    return $return;
}

This is a custom function that will take just the two default parameters being passed to functions inside the wpv-conditional shortcode. If what we are displaying is a post, we check directly the length of the $post->post_content.

Now, we just need to register our function and write our conditional. Note that we do not pass any argument to our function, as they will be passed by default:

Checking For Empty Post Content - shortcode
[wpv-conditional if="( wpv_conditional_post_has_content() eq '0' )"]
This post has no content[/wpv-conditional]

Check If A Types Repeating Field Has A N-th Item

Sometimes, checking if a field is empty is not enough. You can store multiple values in a Types repeating field, and you might need to check if such a field has a number of values to show different layouts.

Let’s assume that you are storing images in a repeating field, and you will display the first of them as a featured image but the rest will be wrapped in a container div.secondary-image. In this case, you can not use a wpv-for-each shortcode, since it will render all the fields in the same way and you want to style the first differently. Also, a classic wpv-conditional shortcode will not help either, because it can only check the emptiness of the overall field and not on a concrete index.

Views has the answer for that. Place the following code in your theme’s functions.php file:

Check If A Types Repeating Field Has A N-th Item
function prefix_check_repeating_field_index_exists($field_id, $index, $mode, $object) {
    global $wpcf;
    if ( isset( $wpcf ) && function_exists('types_get_field') && $mode == 'posts' ) {
        $field = types_get_field( $field_id );
        if ( types_is_repetitive( $field ) ) {
            $wpcf->repeater->set( $object->ID, $field );
            $_meta = $wpcf->repeater->_get_meta();
            $meta = $_meta['custom_order'];
            if ( count( $meta ) > $index ) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    } else {
        return 0;
    }
}

Now, you just need to register this function and then you can check for particular indexes within a repeating field. You should have in mind to use the Types field name (without the wpcf- prefix) and that the index starts on 0:

Check If A Types Repeating Field Has A N-th Item - shortcode
[wpv-conditional if="( prefix_check_repeating_field_index_exists('my-image',2) eq 1 )"]
The field has a value in the index 2, meaning the repeating field <code>my-image</code> 
holds at least three images
[/wpv-conditional]

To add custom functions, the “manage_options” capability is required, meaning that only admins can do this.