Conditional Single CPT Templates based on Post Hierarchy in Bricks

In the past we covered Conditional Bricks Templates Based on Product Category Hierarchy Levels in Bricks.

This Pro tutorial provides the steps to programmatically apply a Bricks template created for Level 1 items (posts of a Custom Post Type) to CPT items that are at Level 1 (refer to the sample screenshot below), Level 2 template to all single CPT items that are at Level 2, Level 3 template to all single CPT items that are at Level 3 and so on…

Step 1

If you have not already, create a template that applies to all single posts of your CPT. Ensure that the template condition is set to Post type → Services (or whatever is your post type). This template will be like a fallback and gets used/applied for all the singular CPT pages that do not meet the criterion for the levels we are going to check for in the next step.

Next, create different templates for Level 1, Level 2, Level 3 and so on – as many levels as you need. Do not set any template conditions for these.

When done, it should appear something like this:

Note the IDs of Level 1, 2 and 3 etc. templates. We need these in the next step.

Step 2

Let’s define a custom function to check if the current post’s parent-child hierarchy level is the given level (number starting with 1) and use it with the bricks/active_templates filter.

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

// Function to check if the current post's parent-child hierarchy level is the given level (number starting with 1).
function bl_check_cpt_hierarchy_level( $level ): bool {
	// Get the ancestors of the current post
	$ancestors = get_post_ancestors( get_the_ID() );

	return $level === count( $ancestors) + 1;
}

add_filter( 'bricks/active_templates', function ( $active_templates, $post_id, $content_type ) {
	// if this is not the specified CPT single page or if not the frontend or $content_type is not 'content', return early
	if ( ! is_singular( 'service' ) || ! bricks_is_frontend() || $content_type !== 'content' ) {
		return $active_templates;
	}
	
	if ( bl_check_cpt_hierarchy_level( 1 ) ) {
		$active_templates['content'] = 872;
	} elseif ( bl_check_cpt_hierarchy_level( 2 ) ) {
		$active_templates['content'] = 875;
	} elseif ( bl_check_cpt_hierarchy_level( 3 ) ) {
		$active_templates['content'] = 878;
	}

	return $active_templates;
}, 10, 3 );

Replace 872, 875 and 878 with the IDs of your templates for the corresponding levels.

With the above code in place, any post that is at Level 4 will use the default or fallback template i.e., “Single Service” in this example. If you’d like to define a template for Level 4 items add another elseif branch like this:

if ( bl_check_cpt_hierarchy_level( 1 ) ) {
	$active_templates['content'] = 872;
} elseif ( bl_check_cpt_hierarchy_level( 2 ) ) {
	$active_templates['content'] = 875;
} elseif ( bl_check_cpt_hierarchy_level( 3 ) ) {
	$active_templates['content'] = 878;
} elseif ( bl_check_cpt_hierarchy_level( 4 ) ) {
	$active_templates['content'] = 890;
}

Replace 890 with the ID of your template for Level 4 posts.