Toolset Conditional block allows you to display or hide blocks based on conditions you set. The conditional block can include multiple conditions and they can be nested.
You can test data from a standard or custom field, a Views output or other custom shortcodes and functions. The value can be compared against a fixed value like a number, the value of another custom field, dates, and more.
For example, you can create conditions that check for empty values of custom fields, check for the current user’s role, or check for values in taxonomy terms.
Let’s start with an example. We have a site about cooking recipes. Each recipe displays information about preparation time, level of difficulty, and more.
We want to show a message to our visitors only if a certain dish can be prepared in under 30 minutes.
- We add a Toolset Conditional block to our page. Of course, you can use multiple Conditional blocks on the same page or a template.
- Next, we add a new block within that Conditional block. In this case, this will be a paragraph block but Conditional block can host any other block, including other conditional blocks.
After adding the Conditional block, click the Add new conditions button.
Use the dialog that appears to select what to compare, the comparison operator, and the value to compare against.
In this dialog, you can also set the condition to be negative, add more conditions, link conditions by AND or OR operators, and group conditions.
Next, we need to add content to display when the set conditions are met. To do that, position yourself within the Conditional block and add a new block with your content.
In our example, this is what our visitor will see.
If preparing time is less than 30 minutes
If preparing time is not less than 30 minutes
When a custom field is not mandatory some posts might not have a value for it. If on the front-end, the field has a label or an icon, you don’t want to display them when there is nothing to output. All you need to do is check the field for empty values and put the label and the field output into the Conditional block.
- Insert the Toolset Conditional block.
- In the left dropdown, select the custom field you want to check against.
- In the dropdown to select an operator, select the Empty value option.
Sometimes, you need to display certain content only to users with specific user roles. For example, you might add notices, links, and features that only administrators should see. When editing conditions, in the left dropdown select Current user role. Next, select the operator. Finally, in the right dropdown, select Other role and in the dropdown that appears, select the user role you want to check for.
You can easily create a condition that checks the values from taxonomy terms. When editing conditions, in the left dropdown select: 1. Post Data 2. Post Source can be a current post or related one. 3. For Source, select Post Taxonomies. 4. Under Fields, select the taxonomy you want to test. Next, select the operator. Finally, in the right dropdown, select Static value and then type in the slug of the taxonomy term you want to check against.
Use the Advanced editor switch to set conditions manually. Please note that some conditions might be lost if you switch back to the Visual Editor mode.
You can create conditions based on the output of shortcodes. This includes Views shortcodes and custom shortcodes.
When displaying a list of posts using a View or on an archive page, there are some very useful Views shortcodes you can base your conditionals on.
For example, you can check against the number of items in the loop or check the current item’s index in a loop.
To use this, add or edit a condition and select the Views shortcodes option in the dropdown menu. In the dropdown menu that appears, select the shortcode you need.
To create conditions based on custom shortcodes, you have to register them first.
Go to the Toolset → Settings page and click the Front-end Content tab. There, scroll to the Third-party shortcode arguments and add the name of your custom shortcode.
To use this, add or edit a condition and select the Custom shortcode option in the dropdown menu. Select your shortcode from the dropdown that appears and optionally, enter shortcode attributes.
You can create conditions based on the output of custom PHP functions.
Before using a function inside a conditional, you must register it for security reasons. Even WordPress functions need to be registered.
To do so, visit the Toolset → Settings page and click the Front-end Content tab. There, add your function to the Functions inside conditional evaluations section.
To use this, add or edit a condition and select the Custom function option in the dropdown menu. In the dropdown menu that appears, select the function you need.
Toolset even allows you to add custom PHP code without editing your theme’s files or creating a child theme.
For more information visit the documentation page about using custom functions in conditions.
Toolset Conditional block allows you to easily combine multiple conditions or create nested groups of conditions.
When adding or editing conditions, click the plus button at the bottom of the dialog. You can select to insert another condition or a group.
If you insert a condition, an Add / Or control appears. Use it to select if all conditions need to be met (AND) or are mutually exclusive (OR).
You can combine any number of conditions.
If you insert a group, a nested set of conditions is added. This allows you to evaluate groups of conditions against other separate or grouped conditions.
You can create multiple levels of nested conditions.
Display or hide elements
- Displaying Form Data
- Creating templates to display members-only content
- Creating and Displaying Repeatable Field Groups
- Displaying a Map on A Single Post Template
- Displaying a List of Posts on A Map
- Show all related topics...
- Creating a Custom Search
- Creating a Hero Section
- Creating Sliders with Dynamic Post Content
- Adding Dynamic Content to Your Homepage
- Creating a View
- Creating a Custom Archive Page
- Creating Templates to Display Custom Posts
- Creating Responsive Columns and Grids
- Creating Responsive Designs
- Advanced Styling with Container Blocks
- Using Third-Party Hidden Fields