14th Nov '22
/
0 comments

Dynamic Post Sliders in Bricks with BricksExtras and WP Grid Builder

This article provides the answer to

We are going to set up a Bricks query loop that shows the posts of a Custom Post Type with BricksExtrasPro Slider in the loop. When the CPT items are filtered by their taxonomy using WP Grid Builder, the sliders will not appear (they will still take up space).

I initially tried to solve this using the change event, but the correct one to use is appended.

Quoting WP Grid Builder developer, Loïc Blascos:

About the slider, you should not use “change” event but “appended” event: https://docs.wpgridbuilder.com/resources/event-facet-appended/
The DOM will only be available on appended.

When you filter, the old content is removed from the page and the new content is appended.
So you can’t refresh something. You have to initialize the script again for it to work.
And this can only be done when the new content is present in the page (appended event).

Here’s how the CPT archive template is structured:

The Facet element is via WPGB’s Bricks’ add-on.

The two Slide elements in the Pro Slider were deleted, Gallery mode was enabled and a Pro Slider Gallery element was added inside set to

{acf_testimonial_gallery}

where testimonial_gallery is a Gallery type of custom field attached to testimonials using ACF Pro.

Add the JavaScript that calls the BricksExtras’ function that creates the slider.

Settings (gear icon) → PAGE SETTINGS → CUSTOM CODE → Body (footer) scripts:

<script>
window.WP_Grid_Builder &&
WP_Grid_Builder.on("init", function (wpgb) {
    wpgb.facets &&
    wpgb.facets.on("appended", function (args) {
        doExtrasSlider(document.getElementById("brxe-roadst"))
    })
})
</script>

where brxe-roadst is the ID of the query loop’s parent Container.

If you prefer adding the JS via the child theme instead of adding it in the Bricks template, in child themes functions.php inside the anonymous function hooked to wp_enqueue_scripts, add:

if ( is_post_type_archive( 'testimonial' ) ) {
			wp_enqueue_script( 'testimonial', get_stylesheet_directory_uri() . '/assets/js/testimonial.js', [], filemtime( get_stylesheet_directory() . '/assets/js/testimonial.js' ), true );
		}

and the code in assets/js/testimonial.js:

document.addEventListener("DOMContentLoaded", () => {
  window.WP_Grid_Builder &&
	WP_Grid_Builder.on("init", function (wpgb) {
	  wpgb.facets &&
		wpgb.facets.on("appended", function (args) {
		  doExtrasSlider(document.getElementById("brxe-roadst"))
		})
	})
})
Get access to all 632 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

How to Create a Grid Slider in Bricks

How to Create a Grid Slider in Bricks

In this tutorial, we'll learn how to create multiple rows and columns inside the default nested slider of Bricks. Introduction A user recently asked in…
Categories:
How to Create a Right Overflow Effect on Sliders in Bricks

How to Create a Right Overflow Effect on Sliders in Bricks

In this tutorial, we'll learn how to recreate a nice overflow effect on the right of our sliders in Bricks. Introduction Recently, I've been asked…
Categories:
Pro
Testimonials Slider in Bricks

Testimonials Slider in Bricks

Setting up a dynamic testimonial slide that pulls the data from a CPT's post fields and custom fields.
Categories:
Tags:
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…
Populating WS Form Field in a Modal with Button Data Attribute in Bricks

Populating WS Form Field in a Modal with Button Data Attribute in Bricks

How a WS Form field inside of a Modal can be pre-filled by clicking the button to open it.
Categories: