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..

Pro
Filtering Meta Box Cloneable Group by Select Subfield for Multiple Bricks Query Loops with Conditional Output

Filtering Meta Box Cloneable Group by Select Subfield for Multiple Bricks Query Loops with Conditional Output

In the Bricks Facebook group a user asks: Consider this cloneable Meta Box field group for a Custom Post Type called Tour: with the Tour…
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:
Pro
Restrict Content Pro in Bricks

Restrict Content Pro in Bricks

This Pro tutorial provides the steps to restrict Pages and individual elements like Sections in Bricks depending on whether the current user has any active…
Pro
Conditional single post Bricks template based on internal referrer URL

Conditional single post Bricks template based on internal referrer URL

How to automatically select a single post template depending on the page from which the post is accessed.