Setting a Bricks template for Parent pages

This Pro tutorial shows how bricks/active_templates filter can be used to programmatically assign a Bricks template to all singular pages (of any post type) that are parents i.e., have other child pages (of the same post type).

The code snippet can also be modified to only apply to parent posts of a specific post type, if needed.

Add the following in child theme‘s functions.php or a code snippets plugin:

Step 1

Create a Bricks template of “single” type named say “Parent page”.

Edit it and add your desired elements inside.

Go to the list of templates and note the template ID.

Step 2

add_filter( 'bricks/active_templates', function ( $active_templates, $post_id, $content_type ) {
	// If this is not a singular page of any post type, return early
	if ( ! is_singular() ) {
		return $active_templates;
	}

	// Check if the current page has child pages
	$childPagesArray = get_pages( [
		'child_of' => $post_id,
		'post_type' => get_post_type( $post_id ),
	] );

	// Return if not the frontend or $content_type is not 'content' or if the current page has no child pages
	if ( ! bricks_is_frontend() || $content_type !== 'content' || count( $childPagesArray ) === 0 ) {
		return $active_templates;
	}
	
	$active_templates['content'] = 271;

	return $active_templates;
}, 10, 3 );

Replace 271 with the template ID from the previous step.