Gists

by mgibbs189 - 1 year ago

<?php

add_filter( 'facetwp_sort_html', function( $html, $params ) {
    $html = '<div class="facetwp-sort-radio">';
    foreach ( $params['sort_options'] as $key => $atts ) {
        $html .= '<input type="radio" value="' . esc_attr( $key ) . '" /><label>' . esc_html( $atts['label'] ) . '</label>';
    }
    $html .= '</div>';
    return $html;
}, 10, 2 );

by djrmom - 3 years ago

<?php
/**
 * filter html for sort to output radio buttons
 */
add_filter( 'facetwp_sort_html', function( $output, $params ) {
	$output = '<div class="facetwp-sort-radio">';
	foreach ( $params['sort_options'] as $key => $atts ) {
		$output .= '<input type="radio" name="sort" value="' . $key . '"> ' . $atts['label'] . '<br>';
	}
	$output .= '</div>';
	return $output;
}, 10, 2 );

/**
 * js to handle:
 * selecting the correct radio button on load
 * updating the sort when a new button is selected
 */
add_action( 'wp_head', function() {
	?>
	<script>
        (function($) {
            $(document).on('facetwp-loaded', function() {
                if ('undefined' !== typeof FWP.extras.sort ) {
                    $( '.facetwp-sort-radio input:radio[name="sort"]').filter('[value="'+FWP.extras.sort+'"]').prop("checked", true);
                }
            });
            // Sorting
            $(document).on('change', '.facetwp-sort-radio input', function() {
                FWP.extras.sort = $(this).val();
                FWP.soft_refresh = true;
                FWP.autoload();
            });
        })(jQuery);
	</script>
	<?php
}, 100 );
by djrmom - 3 years ago

<?php

/**
 * filter html for sort output to output radio buttons
 */
add_filter( 'facetwp_sort_html', function( $html, $params ) {
    $html = '';
	foreach ( $params['sort_options'] as $key => $atts ) {
		$html .= '<input type="radio" name="custom-sort" value="' . $key . '">' . $atts['label'] . '<br>';
	}
	return $html;
}, 10, 2 );

/**
 * add js to custom handle selections of sort radio button
 */
add_action( 'wp_head', function() {
	?>
    <script>
        (function($) {
            $(document).on('facetwp-loaded', function() {
                $('input[type=radio][name=custom-sort]').change(function () {
                    FWP.extras.sort = $(this).val();
                    FWP.soft_refresh = true;
                    FWP.autoload();
                });
            });
        })(jQuery);
    </script>
	<?php
}, 100 );