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
Conditionally Hiding Bricks Filters based on Select Filter Value

Conditionally Hiding Bricks Filters based on Select Filter Value

How show or hide Bricks' filter based on the selection made by another filter.
Categories:
Tags:
bricks/posts/merge_query Explained

bricks/posts/merge_query Explained

This short article provides details on Bricks' bricks/posts/merge_query filter. On pages rendered by an archive or search template, secondary queries like the ones used for…
Categories:
Tags: