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 612 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
How to hide Bricks Query Loop Posts until WP Grid Builder Facets are applied

How to hide Bricks Query Loop Posts until WP Grid Builder Facets are applied

How to hide the Bricks QL posts until there’s an interaction by the user on the frontend like ticking a category or typing in a…
Categories:
Random Post Inside Modal using BricksExtras

Random Post Inside Modal using BricksExtras

In the BricksLabs Facebook group a user asks: So I have a modal, that opens with a random post title and content from a custom…
Categories:
How to import custom posts with featured images and custom fields by fetching an API using a single AJAX call

How to import custom posts with featured images and custom fields by fetching an API using a single AJAX call

In this tutorial, we will import a list of movies - including images and custom fields - inside a custom post type by fetching a…
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…
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 add a custom SwiperJS slider in Bricks

How to add a custom SwiperJS slider in Bricks

In this tutorial, we'll show you how to integrate a custom slider in Bricks using the SwiperJS library and how to add custom parameters using…
Categories:
Tags:
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: