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 630 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Prefiltering Bricks Terms Query Loop

Prefiltering Bricks Terms Query Loop

Let's say there's a Events CPT with a 'Event Year' custom taxonomy. Sample event year names could be: 2021, 2022, 2024, 2025, 2028 etc. We…
Pro
How to programmatically add a class to an element in Bricks

How to programmatically add a class to an element in Bricks

This Pro tutorial shows how a dynamic class can be added to a query loop element in Bricks programmatically. Let's take an example where posts…
Pro
“Truncate text to these many characters” Bricks Control

“Truncate text to these many characters” Bricks Control

Bricks provides a :<number> dynamic data tag modifier that can be used to limit the amount of text by the specified number of words. Ex.:…
How to create filters with IsotopeJS in Bricks (Part 2): Dynamic Image Galleries

How to create filters with IsotopeJS in Bricks (Part 2): Dynamic Image Galleries

This tutorial series will review how to create a dynamic filterable image gallery using the IsotopeJS library‘s features in Bricks. Requirements Create a custom taxonomy for your…