20th Jul '24
/
0 comments

Bricks Templates Query Loop

In the Bricks forum a user asks:

In the new update in Bricks 1.10
We have templates screenshot feature for templates.
I created a query loop for my templates and I need to call the screenshot of the template in the query loop.
Can anyone help me, please?

Bricks 1.10, currently in beta adds support for Template screenshots.

This setting is disabled by default and can be enabled at Bricks → Settings.

In the Templates dialog inside the editor, screenshots can be generated on-demand (usually for any templates already present before this setting is enabled). Screenshots will be automatically generated for all templates when the setting is on.

If you are looking to output the template names in a Bricks query loop with their screenshots, it is not enough to simply set the Image element’s source to the featured image. This is because the presence of featured image is first checked for the templates and if it is not present, automatically-generated template screenshots are pulled. Templates will typically be using auto-generated screenshots.

The logic for this is defined in /wp-content/themes/bricks/includes/admin.php.

Let’s define a custom function with the above code.

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php

function bl_get_bricks_template_image_url(): ?string {
    $post_id = get_the_ID();

    // Check if post has a thumbnail
    if ( has_post_thumbnail( $post_id ) ) {
        return get_the_post_thumbnail_url( $post_id, 'thumbnail' );
    } else {
        // Get automatically-generated template screenshot from custom directory
        $wp_upload_dir = wp_upload_dir();
        $custom_dir = $wp_upload_dir['basedir'] . '/' . BRICKS_TEMPLATE_SCREENSHOTS_DIR . '/';
        $custom_url = $wp_upload_dir['baseurl'] . '/' . BRICKS_TEMPLATE_SCREENSHOTS_DIR . '/';
        $existing_files = glob($custom_dir . "template-screenshot-$post_id-*.webp");
        $file_path = $existing_files[0] ?? null;

        if ( $file_path ) {
            return str_replace( $custom_dir, $custom_url, $file_path );
        }
    }

    // Return null if no image is found
    return null;
}

Next, whitelist the bl_get_bricks_template_image_url function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_get_bricks_template_image_url'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_get_bricks_template_image_url. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Set up a query loop with this PHP in the query editor:

return [
	'post_type' => 'bricks_template',
	'posts_per_page' => 100, // a large number
	'no_found_rows' => true,
];

Sign code.

For showing the template screenshot, add an Image element and set its source to:

{echo:bl_get_bricks_template_image_url}

Get access to all 614 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Post Views Counter Query Loop in Bricks

Post Views Counter Query Loop in Bricks

How we can output the most viewed posts in your Bricks site in a query loop.
Categories:
Pro
Displaying Gallery of SureCart Product Images with Bricks’ Query Loop

Displaying Gallery of SureCart Product Images with Bricks’ Query Loop

Custom query loop for looping through SureCart product images.
Categories:
Tags:
How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

This tutorial provides the PHP & JS codes that can be pasted in order to create a flying effect on map markers each time your…
Pro
Conditionally Outputting Query Loop Item Based on Post Meta in Bricks

Conditionally Outputting Query Loop Item Based on Post Meta in Bricks

Rendering query-loop enabled posts depending on the value of each post's custom field value is tricky because by default, the custom field plugins' functions or…
Categories:
How to create a dynamic infinite logo slider with ACF and Bricks

How to create a dynamic infinite logo slider with ACF and Bricks

This tutorial provides the builder settings, PHP & CSS codes that can be pasted in order to create an infinite logo slider in pure CSS…
Pro
Excluding Duplicate Posts from Query Loops in Bricks

Excluding Duplicate Posts from Query Loops in Bricks

How to ensure that posts are not duplicated when two query loops are used on the same page.
Categories:
Pro
Non-empty events sorted by event date and other posts below in Bricks

Non-empty events sorted by event date and other posts below in Bricks

Updated on 15 Jan 2024 Consider a scenario where you are showing posts from multiple post types namely post, event and game in a single…
Categories:
Using WP Grid Builder Facets with Bricks’ Query Loop

Using WP Grid Builder Facets with Bricks’ Query Loop

Update: This tutorial was written before WPGB had a Bricks add-on. Now, it is advisable to use that add-on instead. Get it from here. Updated…
Categories: