The Toolset Conditional block allows you to control the visibility of any WordPress blocks you put inside it. You can create conditions based on custom fields, taxonomies, user roles, and other options.
You can use the Toolset Conditional block inside posts and pages, templates, lists of posts, or archive loops. This makes it a powerful way to customize your site without programming.
What You’ll Learn
This tutorial teaches the following 5 applications of the conditional block:
When the condition you set is met, the contents of the block are displayed.
When the condition is not fulfilled, you won’t see anything of what you put inside the block.
Here are 5 interesting applications of using the Toolset Conditional block that will help you understand the power it brings to your website development. You can easily use these ideas as starting points and customize them in any way you need.
#1 Label Some Posts with Special Text
You can use the Conditional block inside the Toolset View block that lists your posts and make some of them stand out or display important, additional information.
In our example, we used the Speakers and Events demo site. We display a list of upcoming events and flag those that are free with a green label. At the same time, events that provide live streaming are marked with a red label.
How-to Steps
Events are a custom post type on our site and here are the steps to display conditional labels:
Add a custom Checkbox field to your post type. We named ours “Live stream”.
Edit the posts you want to feature, and put a check in that custom field.
Insert the Conditional block inside your View Loop and click to add new conditions.
In our case, we set up the following condition:
In the first dropdown, select Post data.
In the Post Source dropdown, select Current Event.
In the Source dropdown, select our Field Group for Events.
In the Field dropdown, select our “Live stream” checkbox field.
In the middle dropdown, select the equal operator because we want to check when this field is equalto a specific value.
Finally, in the right hand dropdown, select Static value and then type in the number 1. This is because selected checkbox fields have the value of 1 saved in the database.
So, our condition is saying: show the contents of this block only if the current post of the loop has the “Live stream” field checked.
Add the Toolset Heading block inside your Conditional block with the title of your choice, reflecting the meaning of your custom field, e.g. “Free entry”.
Set up and style the heading block according to your needs. For example, you can set it as a paragraph with a green background and rounded corners
#2 Feature Some Products with a Best Seller Label
You can display a custom label in the same way as above but this time, do it for WooCommerce products. For example, you can create Amazon-like layouts featuring best-selling products:
How-to Steps
The steps are very similar to the ones described above:
Add a custom Checkbox field to the Product post type. You can name it “Best Seller”.
Select a few products you want to feature as best sellers, open them for editing, and check the “Best Seller” field.
Insert the Conditional block field and click to set up the conditions:
Toggle the NOT option on the top left. This time, we’ll check for posts that do not have an empty value saved for the “Best Seller” checkbox.
In the first dropdown, select Post data.
In the Post Source dropdown, select Current Product.
In the Source dropdown, select our Field Group for Products.
In the Field dropdown, select our “Best Seller” checkbox field.
In the middle dropdown, select the Empty operator.
To sum up, our condition will be true for posts that do not have an empty “Best Seller” field.
Insert the Toolset Button block with the “Best Seller” label.
Style your Button block the way you want. For example, you can use the border section to round the top left corner of the button
#3 Display a Submission Form Only if the Logged-in User Hasn’t Submitted Any Posts Yet
You can use the Conditional block to prevent the logged-in user from submitting more than one post of a given type.
In our example, we used the Dog Walkers demo site. Here’s an example of an account page with the form for submitting user’s personal information.
And here’s the same page when the current user already submitted the form so the page displays the user’s information.
How-To Steps
Follow these steps:
Create a View that lists posts you want. In our example, it’s “Dog walkers”.
Select the View block and expand the Content Selection section. Click to add a query filter that displays only posts of the currently logged-in user (the author is the same as the current post in the loop).
Insert the Conditional block inside your View but outside of the View Loop block.
We can check if the View has returned any posts at all. Set up the condition as follows:
In the first dropdown, select Views Shortcode.
In the dropdown that appears, select Number of items found in Views.
In the middle dropdown, select the equal operator.
And in the right dropdown, select Static value and type in 0 (zero).
The condition checks if the number of the items returned by the View is zero. If so, the condition is met the Conditional block will display anything you put inside it.
Insert a submission form inside your Conditional block.
And that’s it, this setup will display the form only if a user hasn’t submitted any posts yet. It will prevent users from posting more than one post.
Note that you can use a similar condition to prevent a user from submitting more than X posts. Simply compare the number of items found with the number of your choice.
#4 Feature Posts With a Specific Taxonomy Term
Another handy thing you can do with the conditional block is to test against a specific taxonomy term (or a category name) and style it the way you want.
We want to display the “Family sessions” label and an icon only for posts that have the “Family term” assigned.
How-to Steps
We use the following steps:
Create a View for displaying your therapists.
Insert the Conditional block inside the View Loop.
Set up the conditions. In our case, we need the following:
In the first dropdown, select Post data.
In the Post Source dropdown, select Current Therapist.
In the Source dropdown, select Post Taxonomies.
In the Field dropdown, select our “Types of therapy” taxonomy.
In the middle dropdown, select the Contains option.
Finally, in the righthand dropdown, select Static value and then type in “family”.
The condition checks if any of the terms assigned to the post equals to “family”.
Insert the Toolset Button block inside your Conditional block. Set a label of your choice followed by some text, for example, “Family sessions”.
#5 Display Additional Information Only for the First Item In Your View
The Conditional block lets you check the current index of the View loop and display the content of the first item differently than the other posts.
This allows you to create interesting layouts of your posts. For example, you might want to include the product description and skip the Add To Cart button only for thefirstproduct on your list.
How-to Steps
To create a layout as in the picture above we will use the Collage type of the View output combined with the Conditional block checked against the “Current index in the Views loop”.
We follow these steps:
Create a View. In the second step of the View-creation wizard, select the Collage loop style, click Design to create your own layout, and arrange its items as follows:
Select the View block, expand the Limit and offset section, and under Items to display choose 5.
Insert the Conditional block into the View Loop.
Set up your Conditional block as follows:
In the first dropdown, select Views Shortcode.
In the dropdown that appears, select Current index in the Views loop.
In the middle dropdown, select the equal operator.
And in the right dropdown, select Static value and type in 1.
So, our condition checks if the current post of the View loop is the first one.
Insert the Single Field block inside the Conditional block and set it to display the product description or excerpt. This block will be displayed only for the first product returned by the View.
Add other blocks to your View.
If you want to hide the Add To Cart button for the first product you will need to insert another Conditional block. It should check that the “current index in the Views loop” is not equal to 1.
Wrapping Up
As you can see, the Toolset Conditional block is an extremely powerful way to customize your pages, Views, templates, and archives. And just like Toolset itself, the best part is, you don’t need to do any coding.
Do you use Toolset Conditional block? Did you know any of the things described here? Want to share an interesting use case with everyone?
Let us know in the comments!
There’s More!
This article is a part of an ongoing series where we present interesting and powerful ideas of what you can build with Toolset. We’re publishing these every few weeks, so stay tuned!
The Toolset Conditional block allows you to control the visibility of any WordPress blocks you put inside it. You can create conditions based on custom fields, taxonomies, user roles, and other options. You can use the Toolset Conditional block inside posts and pages, templates, lists of posts, or archive loops. This makes it a powerful…
Great tutorial! Is there any way to dynamically/automatically set the best sellers for #2, based on actual sales figures?
We’re happy to hear you like it. There is no built-in feature in the Conditional block that would handle this automatically. There is the total_sales query filter for Views that allows you to, for example, list products with the number of orders greater than X but this is something else. You might want to try to create a custom shortcode and use the get_total_sales() function from WooCommerce, register your shortcode and then use this shortcode inside the Conditional block. I haven’t tried that though.
Thanks Agnes,
Some interesting design scenarios that are good to know about.
On another note…
I am noticing when I load some of the Toolset news posts in Safari that some images are loading really small with the captions 1 character wide in a long column underneath. I tested with Safari Technology Preview and the same thing happens. Unless there is something wrong with my computer config, I would check this out. I have notice a lot of issues with lazy loading lately, images slow to load or not loading at all to the extent the only lazy loading that is working reliably is the one that is built into SiteGrounds Optimiser plugin.
Thank you Stephen for bringing that up. I opened a ticket and asked our developers to look at it.
I see this too as I read it now on my iPhone.
When I look at this post in Safari on Mac the lazy loading does not work. On Chrome it works after small delay. Maybe lazy loading and Safari is the problem? Better not to use lazy loading until Apple fixes this? I don’t know…
Thank you Martin! You are right, I can see the problem you are describing in Safari too which makes it even more problematic. Our devs will look into it. Thank you.
Is there a way to display the custom field with the highest value out of a group of custom fields?
For example, CF1, CF2, CF3, CF4, CF5. Compare all five custom fields then show the value of the custom field with the highest value.
Hello Stan, there is no built-in “max” function/feature but I’m lacking a bit of context here to better understand what you want to achieve. Feel free to open a ticket on our support forum, describe your case and our supporters might point you in the right direction with some workarounds.
Nice to see a cool example of a conditional statement in a collage layout.
I would like to suggest that conditional statements being used for individual block parameters. Maybe something that would work like the screen size variables.
If I want to display a container and change the border or the background depending on a conditional statement, the only way to do this that I can think of is to have separate conditional statements for every possible condition. If my container is going to contain a lot of information/fields, etc, then I have to create multiple copies of the same thing for every possible condition. I guess I could use a template, but that still adds needless complications. https://p119.p3.n0.cdn.getcloudapp.com/items/7KuEWvvm/06b9b536-3ab8-4a73-a3e0-207dfbe08b51.jpeg?v=c4ce9224dafac7724f5b37b34ab78129
Yes, recreating almost identical content can be tedious. You might want to try reusable blocks unless your content doesn’t include dynamic fields. I cannot think of any better ways now but I find it difficult to picture a real-life scenario of why you need something like this – what are these ‘possible conditions’ in your case? are these some complex expressions involving a few values each?
You can give a comparison table with the “If-So”plugin? https://wordpress.org/plugins/if-so/
Is there a “Paid Membership Pro” in the list of conditions?
Vladimir we don’t have any comparison table with the “If-So” plugin but after taking a quick look I can see that this plugin is more general and access-control oriented while Toolset’s conditional block allows you to check custom fields, taxonomies etc conditionally.
A real world use case would be the job listings on one of my sites.
Currently I’m using Views to display a Bootstrap card and I’m changing the colour of the border depending on the job listing’s status (new, in progress, filled, cancelled, etc).
Each job listing has multiple custom fields that also have conditional statements controlling their display.
I’m in the process of changing the site to blocks but ran into a few roadblocks with the conditional requirements, taxonomies, etc and just stopped until Toolset catches up for anything beyond the very basics.
Darryl, thank you for sharing your example! Makes perfect sense. I think that for such cases it’s better to use the classic Views (where you can control HTML and CSS). I haven’t tried it but I’m thinking about creating a separate class for each status and then using the conditional output feature to provide the div with each class depending on the status. This way you define different versions of the wrapper (HTML divs, one for each status) and the actual output (the most complex part in terms of designing) just once. Maybe that’s exactly what you did. This method doesn’t work with blocks. Using classic Views versus blocks-based ones – both have their pros and cons.
Great tutorial! Is there any way to dynamically/automatically set the best sellers for #2, based on actual sales figures?
We’re happy to hear you like it. There is no built-in feature in the Conditional block that would handle this automatically. There is the total_sales query filter for Views that allows you to, for example, list products with the number of orders greater than X but this is something else. You might want to try to create a custom shortcode and use the get_total_sales() function from WooCommerce, register your shortcode and then use this shortcode inside the Conditional block. I haven’t tried that though.
Thanks Agnes,
Some interesting design scenarios that are good to know about.
On another note…
I am noticing when I load some of the Toolset news posts in Safari that some images are loading really small with the captions 1 character wide in a long column underneath. I tested with Safari Technology Preview and the same thing happens. Unless there is something wrong with my computer config, I would check this out. I have notice a lot of issues with lazy loading lately, images slow to load or not loading at all to the extent the only lazy loading that is working reliably is the one that is built into SiteGrounds Optimiser plugin.
Thank you Stephen for bringing that up. I opened a ticket and asked our developers to look at it.
I see this too as I read it now on my iPhone.
When I look at this post in Safari on Mac the lazy loading does not work. On Chrome it works after small delay. Maybe lazy loading and Safari is the problem? Better not to use lazy loading until Apple fixes this? I don’t know…
Thank you Martin! You are right, I can see the problem you are describing in Safari too which makes it even more problematic. Our devs will look into it. Thank you.
Is there a way to display the custom field with the highest value out of a group of custom fields?
For example, CF1, CF2, CF3, CF4, CF5. Compare all five custom fields then show the value of the custom field with the highest value.
Hello Stan, there is no built-in “max” function/feature but I’m lacking a bit of context here to better understand what you want to achieve. Feel free to open a ticket on our support forum, describe your case and our supporters might point you in the right direction with some workarounds.
Nice to see a cool example of a conditional statement in a collage layout.
I would like to suggest that conditional statements being used for individual block parameters. Maybe something that would work like the screen size variables.
If I want to display a container and change the border or the background depending on a conditional statement, the only way to do this that I can think of is to have separate conditional statements for every possible condition. If my container is going to contain a lot of information/fields, etc, then I have to create multiple copies of the same thing for every possible condition. I guess I could use a template, but that still adds needless complications.
https://p119.p3.n0.cdn.getcloudapp.com/items/7KuEWvvm/06b9b536-3ab8-4a73-a3e0-207dfbe08b51.jpeg?v=c4ce9224dafac7724f5b37b34ab78129
Yes, recreating almost identical content can be tedious. You might want to try reusable blocks unless your content doesn’t include dynamic fields. I cannot think of any better ways now but I find it difficult to picture a real-life scenario of why you need something like this – what are these ‘possible conditions’ in your case? are these some complex expressions involving a few values each?
You can give a comparison table with the “If-So”plugin?
https://wordpress.org/plugins/if-so/
Is there a “Paid Membership Pro” in the list of conditions?
Vladimir we don’t have any comparison table with the “If-So” plugin but after taking a quick look I can see that this plugin is more general and access-control oriented while Toolset’s conditional block allows you to check custom fields, taxonomies etc conditionally.
A real world use case would be the job listings on one of my sites.
Currently I’m using Views to display a Bootstrap card and I’m changing the colour of the border depending on the job listing’s status (new, in progress, filled, cancelled, etc).
Each job listing has multiple custom fields that also have conditional statements controlling their display.
I’m in the process of changing the site to blocks but ran into a few roadblocks with the conditional requirements, taxonomies, etc and just stopped until Toolset catches up for anything beyond the very basics.
Darryl, thank you for sharing your example! Makes perfect sense. I think that for such cases it’s better to use the classic Views (where you can control HTML and CSS). I haven’t tried it but I’m thinking about creating a separate class for each status and then using the conditional output feature to provide the div with each class depending on the status. This way you define different versions of the wrapper (HTML divs, one for each status) and the actual output (the most complex part in terms of designing) just once. Maybe that’s exactly what you did. This method doesn’t work with blocks. Using classic Views versus blocks-based ones – both have their pros and cons.