Display conditions decide where an Elementor Pro Theme Builder template appears. This tutorial adds a condition of your own with PHP and no extra plugin: a template that shows only on posts that have comments.
Step 1: Understand What a Condition Is
Conditions in Elementor let you control where templates appear across your site, for example, showing a header only on specific pages or displaying a section only for logged-in users. In this example, we'll create a condition that displays a template only if the current post has comments.
Step 2: Open Your functions.php File
Head into your active theme folder, usually under wp-content/themes/your-theme-name, and open the functions.php file. We'll be adding our custom condition directly here. You can also place it inside a small custom plugin if you prefer to keep your theme clean.
Step 3: Create the Custom Condition
Add the following code to your functions.php file. This snippet defines a new condition called “Has Commented” that checks if the current post contains comments.
1add_action( 'elementor/theme/register_conditions', function( $conditions_manager ) {
2
3 class Elementor_Has_Commented_Condition extends \ElementorPro\Modules\ThemeBuilder\Conditions\Condition_Base {
4
5 public static function get_type() {
6 return 'singular';
7 }
8
9 public function get_name() {
10 return 'has_commented';
11 }
12
13 public function get_label() {
14 return 'Has Commented';
15 }
16
17 public function check( $args ) {
18 return have_comments();
19 }
20 }
21
22 $conditions_manager->get_condition_instance( 'general' )->register_sub_condition( new Elementor_Has_Commented_Condition() );
23
24 });Here’s a quick breakdown of what’s happening in the code:
- We hook into
elementor/theme/register_conditionsto add our custom condition when Elementor initializes. - We create a class that extends
Condition_Base, which is the foundation for all Elementor Theme Builder conditions. get_type()returns the type of condition. We use'singular'because our condition applies to individual posts.get_name()sets the internal condition name, whileget_label()defines the name shown in the Elementor editor.check()defines the actual logic. Here we simply returntrueif the current post has comments.
Step 4: Test the Condition
Once you’ve added the code, head into the Elementor editor and open any template. Click Conditions, and you should now see your new “Has Commented” option in the dropdown list. Select it, save the template, and it will only display on posts that have comments.
Full Working Example
1add_action( 'elementor/theme/register_conditions', function( $conditions_manager ) {
2
3 class Elementor_Has_Commented_Condition extends \ElementorPro\Modules\ThemeBuilder\Conditions\Condition_Base {
4
5 public static function get_type() {
6 return 'singular';
7 }
8
9 public function get_name() {
10 return 'has_commented';
11 }
12
13 public function get_label() {
14 return 'Has Commented';
15 }
16
17 public function check( $args ) {
18 return have_comments();
19 }
20 }
21
22 $conditions_manager->get_condition_instance( 'general' )->register_sub_condition( new Elementor_Has_Commented_Condition() );
23
24 });Useful Links
Elementor Theme Builder Conditions API documentation:
Elementor Developer Hooks reference:
From here, you can write conditions based on user roles, custom fields or any other check you can make in PHP.
I've worked in SEO for more than ten years and helped over 100 businesses rank higher, mostly local businesses, e-commerce stores and SaaS companies. As co-founder of LPagery I work on local SEO, programmatic SEO and AI search visibility, and I test the strategies in these articles on our own sites and with our customers.
