Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Last active October 20, 2022 20:49
Show Gist options
  • Save mgibbs189/d750915dbbda6b254f2cd16ae5f53c4b to your computer and use it in GitHub Desktop.
Save mgibbs189/d750915dbbda6b254f2cd16ae5f53c4b to your computer and use it in GitHub Desktop.
FacetWP - show reset button only when facets are selected
<script>
/*
Code placement: see the "Javascript" section on https://facetwp.com/how-to-use-hooks/
This assumes that your reset button looks like this:
<a class="my-reset-btn" onclick="FWP.reset()">RESET</a>
*/
(function($) {
$(document).on('facetwp-loaded', function() {
var qs = FWP.build_query_string();
if ( '' === qs ) { // no facets are selected
$('.your-reset-btn').hide();
}
else {
$('.your-reset-btn').show();
}
});
})(jQuery);
</script>
@minty-owen
Copy link

Doesn't work for me

@bencicpatricija
Copy link

FWP.build_query_string() should be replaced with FWP.buildQueryString()

@colincny
Copy link

FWP.buildQueryString() seems to return the entire query string, even if the vars have nothing to do with any FWP filters.

@colincny
Copy link

I find that this code is more accurate:

var in_use = false;

        // see if any facets are in use
        $.each(FWP.facets, function(name, val) {
            if (val.length > 0) {
                in_use = true;
            }
        }

@mgibbs189
Copy link
Author

@colincny Thanks for the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment