Last active
October 20, 2022 20:49
-
-
Save mgibbs189/d750915dbbda6b254f2cd16ae5f53c4b to your computer and use it in GitHub Desktop.
FacetWP - show reset button only when facets are selected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
FWP.build_query_string()
should be replaced with FWP.buildQueryString()
FWP.buildQueryString() seems to return the entire query string, even if the vars have nothing to do with any FWP filters.
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;
}
}
@colincny Thanks for the update!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't work for me