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 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
Bricks Slider Images From ACF Flexible Content Sub Field

Bricks Slider Images From ACF Flexible Content Sub Field

Consider the following field group associated with Pages when using ACF Pro: "Page Fields" field group|_"Page Content" Flexible Content field|__"Layout 1" layout|___"Title" Text field|___"Layout 1…
Synced thumbnail sliders in Bricks using BricksExtras

Synced thumbnail sliders in Bricks using BricksExtras

Setting up post-specific synced/linked thumbnail sliders that show photos from a gallery-type custom field using BricksExtras plugin in Bricks.
Pro
Slow Zoom Effect for Slides in Bricks

Slow Zoom Effect for Slides in Bricks

This Pro tutorial provides the steps to set up a slow zoom effect for slides of nestable slider element in Bricks. Step 1 Add a…
Categories:
Tags:
How to sync two nestable sliders in Bricks

How to sync two nestable sliders in Bricks

In this tutorial, we'll learn how to sync two nestable sliders in Bricks: one will be the main slider and the other one will be…
Categories:
Pro
Ken Burns Background Slideshow in Bricks

Ken Burns Background Slideshow in Bricks

Setting up a hero section, having images that pan and move as a background slideshow with content on top.
Categories:
Tags: