FacetWP – slider based on first letter

mgibbs189

{{gist file=”functions.php” lang=”php”}}
<?php

function fwp_slider_alpha_to_number( $params ) {
if ( ‘letter_slider’ == $params[‘facet_name’] ) {
$first_letter = $params[‘facet_display_value’];
$first_letter = strtoupper( substr( $first_letter, 0, 1 ) );

// get ASCII number, subtract by 65 (=A)
$params[‘facet_value’] = ord( $first_letter ) – 65;
$params[‘facet_display_value’] = ord( $first_letter ) – 65;
}
return $params;
}
add_filter( ‘facetwp_index_row’, ‘fwp_slider_alpha_to_number’ );

function fwp_slider_set_label() {
?>
<script>
(function($) {
$(function() {
wp.hooks.addAction(‘facetwp/set_label/slider’, function($this) {
var facet_name = $this.attr(‘data-name’);
var min = FWP.settings[facet_name][‘lower’];
var max = FWP.settings[facet_name][‘upper’];
var min_alpha = String.fromCharCode(65 + parseInt(min));
var max_alpha = String.fromCharCode(65 + parseInt(max));
var label = (min === max) ? min_alpha : min_alpha + ‘ — ‘ + max_alpha;
$this.find(‘.facetwp-slider-label’).html(label);
});
});
})(jQuery);
</script>
<?php
}
add_action( ‘wp_head’, ‘fwp_slider_set_label’, 999 );
{{/gist}}