2nd Jan '24
/
0 comments

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

Get access to all 610 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Removing Action/Filter Inside a Plugin’s Namespaced Class Constructor

Removing Action/Filter Inside a Plugin’s Namespaced Class Constructor

Recently worked with a friend figuring out how to remove/undo/cancel an add_action() line that's inside a plugin's constructor inside a PHP Class with the file…
Categories:
Tags:
Filtering Breadcrumbs in Bricks

Filtering Breadcrumbs in Bricks

Using the bricks/breadcrumbs/items filter to modify Bricks' breadcrumbs’ element programmatically.
Categories:
Pro
Filtering a Bricks Terms query on Term archive pages

Filtering a Bricks Terms query on Term archive pages

On product category archives, how to show product type names but only those that the current product category has.
Pro
Dynamic Post-specific Templates in Bricks using Meta Box/ACF Select Field

Dynamic Post-specific Templates in Bricks using Meta Box/ACF Select Field

Update on 23 Aug 2023: Added steps for ACF. Bricks builder v1.8.4 introduced an pretty useful filter called bricks/active_templates that flew under the radar. This…