Adds a custom “Children of Page†condition in Elementor Theme Builder, allowing you to apply a template automatically to all child pages of a selected parent page.
🎯 What it’s for:
Used when you have a structured page hierarchy (e.g., Areas We Serve → City Pages) and want to use one dynamic template instead of assigning it manually to each page.
✅ How to Use:
Go to Elementor → Single Post/Page Template → Display Conditions
Select: Include → Pages → Children of Page
Choose the parent page (e.g., Areas We Serve)
✅ Code:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'elementor/theme/register_conditions', function( $conditions_manager ) {
if ( ! class_exists( 'ElementorProModulesThemeBuilderConditionsCondition_Base' ) ) {
return;
}
$singular_condition = $conditions_manager->get_condition( 'singular' );
if ( ! $singular_condition || ! method_exists( $singular_condition, 'register_sub_condition' ) ) {
return;
}
class Techup_Elementor_Children_Of_Page_Condition extends ElementorProModulesThemeBuilderConditionsCondition_Base {
public static function get_type() {
return 'singular';
}
public function get_name() {
return 'children_of_page';
}
public function get_label() {
return __( 'Children of Page', 'textdomain' );
}
public function get_group() {
return 'page';
}
public function get_all_label() {
return __( 'All Child Pages', 'textdomain' );
}
public function check( $args ) {
if ( ! is_page() ) {
return false;
}
$current_post_id = get_queried_object_id();
if ( ! $current_post_id ) {
return false;
}
$current_parent_id = wp_get_post_parent_id( $current_post_id );
// Elementor can sometimes pass different keys depending on version/context.
$selected_parent_id = 0;
if ( ! empty( $args['parent_page_id'] ) ) {
$selected_parent_id = absint( $args['parent_page_id'] );
} elseif ( ! empty( $args['id'] ) ) {
$selected_parent_id = absint( $args['id'] );
}
if ( ! $selected_parent_id ) {
return false;
}
return (int) $current_parent_id === (int) $selected_parent_id;
}
public function get_control_value() {
$parent_page_id = $this->get_settings( 'parent_page_id' );
if ( ! $parent_page_id ) {
return '';
}
return get_the_title( $parent_page_id );
}
protected function register_controls() {
$this->start_controls_section(
'section_parent_page',
[
'label' => __( 'Parent Page', 'textdomain' ),
]
);
$this->add_control(
'parent_page_id',
[
'label' => __( 'Select Parent Page', 'textdomain' ),
'type' => ElementorControls_Manager::SELECT2,
'label_block' => true,
'options' => $this->get_pages_options(),
]
);
$this->end_controls_section();
}
private function get_pages_options() {
$pages = get_pages(
[
'sort_column' => 'post_title',
'sort_order' => 'ASC',
]
);
$options = [];
if ( $pages ) {
foreach ( $pages as $page ) {
$options[ $page->ID ] = $page->post_title . ' (#' . $page->ID . ')';
}
}
return $options;
}
}
$singular_condition->register_sub_condition(
new Techup_Elementor_Children_Of_Page_Condition()
);
}, 20 );
