Skip Navigation

Display Content Conditionally in WordPress

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.

Duration

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.

Inserting a Conditional block

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.

  1. We add a Toolset Conditional block to our page. Of course, you can use multiple Conditional blocks on the same page or a template.
  2. 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.
Adding Conditional block
Adding Conditional block

Setting the conditions

After adding the Conditional block, click the Add new conditions button.

Add new conditions
Add new conditions

Use the dialog that appears to select what to compare, the comparison operator, and the value to compare against.

Editing your condition rules
Editing your condition rules

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.

Adding content to display conditionally

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.

Checking the results

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

Checking for Empty Values

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.

  1. Insert the Toolset Conditional block.
  2. In the left dropdown, select the custom field you want to check against.
  3. In the dropdown to select an operator, select the Empty value option.

Conditions Based on User Roles

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.

Checking for Values from Taxonomy Terms

You can easily create a condition that checks the values from taxonomy terms:

  1. When editing conditions, in the left dropdown select 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.
  5. Next, select the operator.
  6. Finally, in the right dropdown, select Static value and then type in the slug of the taxonomy term you want to check against.

Advanced Editor mode

Advanced Editor Mode

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.

Checking for Shortcodes Values

You can create conditions based on the output of shortcodes. This includes Views shortcodes and custom shortcodes.

Views 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.

Custom shortcodes

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.

Checking for Values from Custom Functions

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.

Nesting and Combining 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.

Combining conditions

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.

Nested groups 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.

Main Toolset Block Used in this Lesson

Display or hide elements

Updated
November 18, 2020