13th Mar '23
/
0 comments

Filtering Pro Slider Slides with WPGridBuilder

Filtering Pro Slider Slides with WPGridBuilder

We recently built-in support for many of the BricksExtras elements for being filtered by WPGridBuilder. This means adding Pro Sliders (and other elements) inside of the query loops that are being filtered dynamically with facets will just work out of the box.

But what if we wanted some facets to dynamically change the slides inside of an active slider depending on the results of the facets? This is how we can do it..

(Note this is only for when Bricks’ query loop is being used to populate the slides, not for static sliders or galleries)

End Result

The facets can be used to filter the slides themselves.

Notice that we’re also taking advantage of the ‘conditional slider’ setting from the Pro Slider. Meaning when there aren’t enough slides to fill the available space, the slider features disappear and we’re just left with a regular horizontal flex layout. The dots, the pagination, and the slider counter are no longer visible and also nothing is draggable.

Setting up the Facets

Using WPGridBuilder’s Bricks addon makes this really simple. The process when using with a slider is no different to when using with any other query loop. Simply add in WPGridBuilder’s facet element, choose the facet type and then select the element to filter.

In this case, the element to filter is the block being used as the slide inside of the Pro Slider where the query loop has been set up.

The part that we need to adjust, which is why this tutorial exists, is that the slider needs to be then refreshed each time slides have been removed or added. The number of slides has changed, so the slider and the controllers need to reflect that.

Code Snippet

Most of the logic is already built into the slider already, including the Pro Slider controllers. When the slider is refreshed, they’ll know what to do. So all we’re actually needing to do is the final step, which is just to refresh when the facets are used.

This code will need to be added either inside of a child theme or code snippet plugin.

The part that needs changing is to replace ‘egnswy’ with your own slider’s unique identifier (refer to the developer docs for the Pro Slider on how to get that)

add_action('wp_footer', 'bl_refresh_slider_after_facets', 99);
function bl_refresh_slider_after_facets() { ?>
	
<script>

	const sliderIdentifier = 'egnswy'; /* change to your slider identifier */

	window.WP_Grid_Builder && WP_Grid_Builder.on('init', function(wpgb) {

     wpgb.facets.on('appended', function(content) {

		content.forEach((container) => {

		    const mySlider = xSlider.Instances[sliderIdentifier]

		    if (null != mySlider) {
			mySlider.refresh()
		    }

		})

	    })
		
})		 
	
</script>

<?php
	
};

That’s it.

Now WPGB will take care of adding and removing the slides dynamically, and the slider and the controller will refresh at the same time as the facets to make sure everything is behaving correctly for the new number of slides.

Get access to all 633 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

How to Create a Progress Bar for the Nested Slider in Bricks

How to Create a Progress Bar for the Nested Slider in Bricks

In this tutorial, we'll learn how to add a progress bar to our nested slider element in Bricks. Introduction A user recently asked in the…
Categories:
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:
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
WP Grid Builder Filters inside Bricks’ OffCanvas

WP Grid Builder Filters inside Bricks’ OffCanvas

This Pro tutorial provides the steps to move WP Grid Builder's filters inside the Bricks' native Offcanvas on smaller viewport widths. Let's work with the…
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:
Dynamic Post Galleries in Lightboxes using BricksExtras

Dynamic Post Galleries in Lightboxes using BricksExtras

How to show post-specific galleries as sliders using BricksExtras when a “Open Gallery” link is clicked in post cards in a posts grid.
Categories: