Show/Hide (condition) element if Post has Bricks content

With a little bit of help from ChatGPT, I have come to a solution!

I’ll write it down in case it can help someone in the future:

Step 1. Create a PHP function to check if a Post has _bricks_template_type equals to content.

// Function to check if a post has the "_bricks_template_type" meta field
function has_bricks_template($post_id = null) {
    if (!$post_id) {
        $post_id = get_the_ID();
    }

    if (!$post_id || !is_numeric($post_id)) {
        return 'false';
    }

    // Get the value of "_bricks_template_type"
    $template_type = get_post_meta($post_id, '_bricks_template_type', true);

    // Check if it exists and equals "content"
    return ($template_type === 'content') ? 'true' : 'false';
}

Step 2. Register the custom function to be able to use it in {echo} tag

// Allow the function to be used in Bricks conditions
add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'has_bricks_template',
  ];
} );

Step 3. Add a condition to the element you want to show/hide based on the existence of Bricks content

Condition: {echo:has_bricks_template} == true

If someone has a better solution, please let me know! :slight_smile:

1 Like