Designing individual posts with Bricks

Generally speaking, a template is used for determining the single views of standard Posts and Custom Post Type items in WordPress.

When using Bricks, coming to the standard Pages, it is common to design (some/most) Pages individually with the Bricks editor.

Did you know that Bricks also enables us to design individual posts (of any post type) with the Bricks editor overriding the template for that post type?

Step 1

Go to Bricks → General → Post types.

Select the post types that you wish to be able to edit with Bricks.

In this example, let’s go with a property CPT items.

You should now be able to edit the individual posts (of the enabled post types from the above list) with Bricks.

But your newly edited individual item(s) won’t render the Bricks’ content on the front end yet.

Step 2

This step is only needed when there is a single template that acts like a fallback for the CPT.

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

<?php 

add_filter( 'bricks/active_templates', function ( $active_templates, $post_id, $content_type ) {
	// Only run my logic on the frontend
	if ( ! bricks_is_frontend() ) {
		return $active_templates;
	}

	// Return if single post $content_type is not 'content'
	if ( $content_type !== 'content' ) {
		return $active_templates;
	}

	// Return: Current post type is not 'property'
	$post_type = get_post_type( $post_id );

	if ( $post_type !== 'property' ) {
		return $active_templates;
	}

	// Check if the current post has Bricks data, return value is an array
	$bricks_data = \Bricks\Database::get_data( $post_id, 'content' );

	if ( count( $bricks_data ) > 0 ) {
		// Has Bricks data: Don't use any template, set the $active_templates['content'] to current post ID
		$active_templates['content'] = $post_id;
	}

	return $active_templates;
}, 10, 3 );

Note: If the code is being added at the end of child theme’s functions.php, do not include the opening PHP tag (first line) in the above code.

Replace

property

in

if ( $post_type !== 'property' ) {

with the identifier of your post type.

Source

Instant access to 390+ Bricks code tutorials with BricksLabs Pro

Leave the first comment